Skip to content

Commit

Permalink
Move MatcherSpecInvalidError to exceptions.py
Browse files Browse the repository at this point in the history
  • Loading branch information
c0llab0rat0r authored and ntninja committed May 6, 2021
1 parent 09cae76 commit e656e08
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
31 changes: 24 additions & 7 deletions ipfshttpclient/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
│ ├── EncoderMissingError
│ ├── EncodingError
│ └── DecodingError
└── CommunicationError
├── ProtocolError
├── StatusError
├── ErrorResponse
│ └── PartialErrorResponse
├── ConnectionError
└── TimeoutError
├── CommunicationError
│ ├── ProtocolError
│ ├── StatusError
│ ├── ErrorResponse
│ │ └── PartialErrorResponse
│ ├── ConnectionError
│ └── TimeoutError
└── MatcherSpecInvalidError
"""
import typing as ty
Expand Down Expand Up @@ -105,6 +106,22 @@ def __init__(self, encoder_name: str, original: Exception) -> None:
super().__init__("Object decoding error: {}".format(original), encoder_name)


##################
# filescanner.py #
##################

class MatcherSpecInvalidError(TypeError):
"""
An attempt was made to build a matcher using matcher_from_spec, but an invalid
specification was provided.
"""

def __init__(self, matcher_class: type, invalid_spec: ty.Any) -> None:
super().__init__(
f"Don't know how to create a {matcher_class.__name__} from spec {invalid_spec!r}"
)


###########
# http.py #
###########
Expand Down
11 changes: 3 additions & 8 deletions ipfshttpclient/filescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# To encourage contributions from PyCharm users, we redefine AnyStr.
#
# This will get inlined if/when PyCharm no longer flags typing.AnyStr.
from .exceptions import MatcherSpecInvalidError

AnyStr = ty.TypeVar('AnyStr', bytes, str)

if sys.version_info >= (3, 7): #PY37+
Expand Down Expand Up @@ -413,13 +415,6 @@ def should_report(self, path: AnyStr, *, is_dir: bool) -> bool:
]


class MatcherSpecInvalidError(TypeError):
def __init__(self, invalid_spec: ty.Any) -> None:
super().__init__(
f"Don't know how to create a {Matcher.__name__} from spec {invalid_spec!r}"
)


def _require_spec(spec: ty.Optional[match_spec_t[AnyStr]]) -> match_spec_t[AnyStr]:
"""
Assist the type checker by narrowing the number of places accepting Optional.
Expand Down Expand Up @@ -502,7 +497,7 @@ def _recursive_matcher_from_spec(spec: match_spec_t[AnyStr], *,
else: # Actual list of matchers (plural)
return MetaMatcher(matchers)
else:
raise MatcherSpecInvalidError(spec)
raise MatcherSpecInvalidError(Matcher, spec)


class walk(ty.Generator[FSNodeEntry[AnyStr], ty.Any, None], ty.Generic[AnyStr]):
Expand Down
4 changes: 3 additions & 1 deletion test/unit/test_filescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import pytest

from datetime import datetime

from ipfshttpclient import filescanner

from ipfshttpclient.exceptions import MatcherSpecInvalidError
from ipfshttpclient.filescanner import FSNodeEntry
from ipfshttpclient.filescanner import FSNodeType

Expand Down Expand Up @@ -164,7 +166,7 @@ def test_glob_matching(

@pytest.mark.parametrize('spec', [123, datetime.now()])
def test_matcher_from_spec_rejects_invalid_spec_type(spec: ty.Any) -> None:
with pytest.raises(filescanner.MatcherSpecInvalidError):
with pytest.raises(MatcherSpecInvalidError):
filescanner.matcher_from_spec(spec)


Expand Down

0 comments on commit e656e08

Please sign in to comment.