Skip to content

Commit

Permalink
chore: Bump mypy and related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam committed Oct 2, 2024
1 parent fdaee8b commit 9f16a0a
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test = [
]
type-check = [
"phantom-types[all]",
"mypy==1.4.1",
"mypy",
"pytest",
"types-python-dateutil",
]
Expand Down
2 changes: 1 addition & 1 deletion src/phantom/_utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def is_not_known_mutable_type(type_: BoundType) -> TypeGuard[NotKnownMutableType
return not (
any(is_subtype(type_, mutable_type) for mutable_type in mutable)
or (
is_dataclass(type_) and not type_.__dataclass_params__.frozen # type: ignore[union-attr]
is_dataclass(type_) and not type_.__dataclass_params__.frozen # type: ignore[attr-defined]
)
)

Expand Down
30 changes: 29 additions & 1 deletion src/phantom/_utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ class SupportsGe(
@runtime_checkable
class _SupportsEq(Protocol):
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...


class SupportsEq(Protocol, metaclass=CachingProtocolMeta): ...
class SupportsEq(
_SupportsEq,
Protocol,
metaclass=CachingProtocolMeta,
): ...


@runtime_checkable
Expand All @@ -82,6 +87,29 @@ class Comparable(
): ...


@runtime_checkable
class _SupportsFloat(Protocol):
def __float__(self) -> float: ...


class SupportsFloat(_SupportsFloat, Protocol, metaclass=CachingProtocolMeta): ...


@runtime_checkable
class _FloatComparable(
SupportsFloat,
Comparable[T_contra],
Protocol[T_contra],
): ...


class FloatComparable(
_FloatComparable[T_contra],
Protocol[T_contra],
metaclass=CachingProtocolMeta,
): ...


@runtime_checkable
class _SupportsLeGe(SupportsLe[T_contra], SupportsGe[T_contra], Protocol[T_contra]): ...

Expand Down
4 changes: 2 additions & 2 deletions src/phantom/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def parse(cls, instance: object) -> TZAware:
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A date-time with timezone data.",
}

Expand All @@ -102,7 +102,7 @@ def parse(cls, instance: object) -> TZNaive:
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A date-time without timezone data.",
"format": "date-time-naive",
}
Expand Down
4 changes: 2 additions & 2 deletions src/phantom/ext/phonenumbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class PhoneNumber(str, Phantom, predicate=is_phone_number):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A valid E.164 phone number.",
"type": "string",
"format": "E.164",
Expand All @@ -100,6 +100,6 @@ def parse(cls, instance: object) -> FormattedPhoneNumber:
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"title": "PhoneNumber",
}
43 changes: 22 additions & 21 deletions src/phantom/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def take_portion(portion: Portion, whole: Natural) -> float:
from . import _hypothesis
from ._utils.misc import resolve_class_attr
from ._utils.types import Comparable
from ._utils.types import FloatComparable
from ._utils.types import SupportsEq
from .predicates import interval
from .schema import Schema
Expand Down Expand Up @@ -93,13 +94,13 @@ def _get_scalar_float_bounds(

if low is not None:
try:
low = float(low) # type: ignore[arg-type]
low = float(low)
except TypeError as excpetion:
raise _NonScalarBounds from excpetion

if high is not None:
try:
high = float(high) # type: ignore[arg-type]
high = float(high)
except TypeError as exception:
raise _NonScalarBounds from exception

Expand Down Expand Up @@ -136,14 +137,14 @@ class Interval(Phantom[Comparable], bound=Comparable, abstract=True):
"""

__check__: IntervalCheck
__low__: Comparable
__high__: Comparable
__low__: FloatComparable
__high__: FloatComparable

def __init_subclass__(
cls,
check: IntervalCheck | None = None,
low: Comparable | None = None,
high: Comparable | None = None,
low: FloatComparable | None = None,
high: FloatComparable | None = None,
**kwargs: Any,
) -> None:
_resolve_bound(cls, "__low__", low, neg_inf)
Expand Down Expand Up @@ -177,13 +178,13 @@ class Exclusive(Interval, check=interval.exclusive, abstract=True):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": (
f"A value in the exclusive range ({_format_limit(cls.__low__)}, "
f"{_format_limit(cls.__high__)})."
),
"exclusiveMinimum": cls.__low__ if cls.__low__ != neg_inf else None,
"exclusiveMaximum": cls.__high__ if cls.__high__ != inf else None,
"exclusiveMinimum": float(cls.__low__) if cls.__low__ != neg_inf else None,
"exclusiveMaximum": float(cls.__high__) if cls.__high__ != inf else None,
}

@classmethod
Expand All @@ -209,13 +210,13 @@ class Inclusive(Interval, check=interval.inclusive, abstract=True):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": (
f"A value in the inclusive range [{_format_limit(cls.__low__)}, "
f"{_format_limit(cls.__high__)}]."
),
"minimum": cls.__low__ if cls.__low__ != neg_inf else None,
"maximum": cls.__high__ if cls.__high__ != inf else None,
"minimum": float(cls.__low__) if cls.__low__ != neg_inf else None,
"maximum": float(cls.__high__) if cls.__high__ != inf else None,
}

@classmethod
Expand All @@ -237,13 +238,13 @@ class ExclusiveInclusive(Interval, check=interval.exclusive_inclusive, abstract=
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": (
f"A value in the half-open range ({_format_limit(cls.__low__)}, "
f"{_format_limit(cls.__high__)}]."
),
"exclusiveMinimum": cls.__low__ if cls.__low__ != neg_inf else None,
"maximum": cls.__high__ if cls.__high__ != inf else None,
"exclusiveMinimum": float(cls.__low__) if cls.__low__ != neg_inf else None,
"maximum": float(cls.__high__) if cls.__high__ != inf else None,
}

@classmethod
Expand All @@ -265,13 +266,13 @@ class InclusiveExclusive(Interval, check=interval.inclusive_exclusive, abstract=
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": (
f"A value in the half-open range [{_format_limit(cls.__low__)}, "
f"{_format_limit(cls.__high__)})."
),
"minimum": cls.__low__ if cls.__low__ != neg_inf else None,
"exclusiveMaximum": cls.__high__ if cls.__high__ != inf else None,
"minimum": float(cls.__low__) if cls.__low__ != neg_inf else None,
"exclusiveMaximum": float(cls.__high__) if cls.__high__ != inf else None,
}

@classmethod
Expand All @@ -293,7 +294,7 @@ class Natural(int, InclusiveExclusive, low=0):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "An integer value in the inclusive range [0, ∞).",
}

Expand All @@ -304,7 +305,7 @@ class NegativeInt(int, ExclusiveInclusive, high=0):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "An integer value in the inclusive range (-∞, 0].",
}

Expand All @@ -315,6 +316,6 @@ class Portion(float, Inclusive, low=0, high=1):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A float value in the inclusive range [0, 1].",
}
2 changes: 1 addition & 1 deletion src/phantom/iso3166.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def parse(cls, instance: object) -> ParsedAlpha2:
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "ISO3166-1 alpha-2 country code",
"examples": ["NR", "KZ", "ET", "VC", "AE", "NZ", "SX", "XK", "AX"],
"format": "iso3166-1 alpha-2",
Expand Down
4 changes: 3 additions & 1 deletion src/phantom/negated.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def __register_strategy__(cls) -> _hypothesis.HypothesisStrategy:
from hypothesis.strategies import from_type
from hypothesis.strategies import tuples

def create_strategy(type_: type[T]) -> _hypothesis.SearchStrategy[T] | None:
def create_strategy(
type_: type[T],
) -> _hypothesis.SearchStrategy[tuple[T, ...]] | None:
(inner_type,) = get_args(type_)
return tuples(from_type(inner_type))

Expand Down
4 changes: 2 additions & 2 deletions src/phantom/re.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init_subclass__(cls, pattern: Pattern[str] | str, **kwargs: Any) -> None:
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": (
"A string starting with a match of the format regular expression."
),
Expand All @@ -71,7 +71,7 @@ def __init_subclass__(cls, pattern: Pattern[str] | str, **kwargs: Any) -> None:
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A string that matches the format regular expression.",
"format": str(cls.__pattern__.pattern),
}
Expand Down
12 changes: 6 additions & 6 deletions src/phantom/sized.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init_subclass__(
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"type": "array",
}

Expand Down Expand Up @@ -188,14 +188,14 @@ def __init_subclass__(
def __schema__(cls) -> Schema:
return (
{
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"type": "string",
"minLength": cls.__min__,
"maxLength": cls.__max__,
}
if str in cls.__mro__
else {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"type": "array",
"minItems": cls.__min__,
"maxItems": cls.__max__,
Expand Down Expand Up @@ -241,7 +241,7 @@ class NonEmpty(PhantomBound[T], Generic[T], min=1):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A non-empty array.",
}

Expand All @@ -252,7 +252,7 @@ class NonEmptyStr(str, NonEmpty[str]):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "A non-empty string.",
}

Expand All @@ -263,7 +263,7 @@ class Empty(PhantomBound[T], Generic[T], max=0):
@classmethod
def __schema__(cls) -> Schema:
return {
**super().__schema__(), # type: ignore[misc]
**super().__schema__(),
"description": "An empty array.",
}

Expand Down
6 changes: 2 additions & 4 deletions tests/predicates/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ class TestFunctionRepr:
def test_explodes_partial_arguments(self):
predicate = partial(foo, 10, b=5)
assert_predicate_name_equals(boolean.negate(predicate), "negate(foo(10, b=5))")
predicate = partial(foo, "hello", c="goddag")
assert_predicate_name_equals(
boolean.negate(predicate), "negate(foo('hello', c='goddag'))"
)
predicate = partial(foo, 23, c=31)
assert_predicate_name_equals(boolean.negate(predicate), "negate(foo(23, c=31))")
55 changes: 28 additions & 27 deletions typing-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,34 @@ iniconfig==2.0.0 \
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
# via pytest
mypy==1.4.1 \
--hash=sha256:01fd2e9f85622d981fd9063bfaef1aed6e336eaacca00892cd2d82801ab7c042 \
--hash=sha256:0dde1d180cd84f0624c5dcaaa89c89775550a675aff96b5848de78fb11adabcd \
--hash=sha256:141dedfdbfe8a04142881ff30ce6e6653c9685b354876b12e4fe6c78598b45e2 \
--hash=sha256:16f0db5b641ba159eff72cff08edc3875f2b62b2fa2bc24f68c1e7a4e8232d01 \
--hash=sha256:190b6bab0302cec4e9e6767d3eb66085aef2a1cc98fe04936d8a42ed2ba77bb7 \
--hash=sha256:2460a58faeea905aeb1b9b36f5065f2dc9a9c6e4c992a6499a2360c6c74ceca3 \
--hash=sha256:34a9239d5b3502c17f07fd7c0b2ae6b7dd7d7f6af35fbb5072c6208e76295816 \
--hash=sha256:43b592511672017f5b1a483527fd2684347fdffc041c9ef53428c8dc530f79a3 \
--hash=sha256:43d24f6437925ce50139a310a64b2ab048cb2d3694c84c71c3f2a1626d8101dc \
--hash=sha256:45d32cec14e7b97af848bddd97d85ea4f0db4d5a149ed9676caa4eb2f7402bb4 \
--hash=sha256:470c969bb3f9a9efcedbadcd19a74ffb34a25f8e6b0e02dae7c0e71f8372f97b \
--hash=sha256:566e72b0cd6598503e48ea610e0052d1b8168e60a46e0bfd34b3acf2d57f96a8 \
--hash=sha256:5703097c4936bbb9e9bce41478c8d08edd2865e177dc4c52be759f81ee4dd26c \
--hash=sha256:7549fbf655e5825d787bbc9ecf6028731973f78088fbca3a1f4145c39ef09462 \
--hash=sha256:8207b7105829eca6f3d774f64a904190bb2231de91b8b186d21ffd98005f14a7 \
--hash=sha256:8c4d8e89aa7de683e2056a581ce63c46a0c41e31bd2b6d34144e2c80f5ea53dc \
--hash=sha256:98324ec3ecf12296e6422939e54763faedbfcc502ea4a4c38502082711867258 \
--hash=sha256:9bbcd9ab8ea1f2e1c8031c21445b511442cc45c89951e49bbf852cbb70755b1b \
--hash=sha256:9d40652cc4fe33871ad3338581dca3297ff5f2213d0df345bcfbde5162abf0c9 \
--hash=sha256:a2746d69a8196698146a3dbe29104f9eb6a2a4d8a27878d92169a6c0b74435b6 \
--hash=sha256:ae704dcfaa180ff7c4cfbad23e74321a2b774f92ca77fd94ce1049175a21c97f \
--hash=sha256:bfdca17c36ae01a21274a3c387a63aa1aafe72bff976522886869ef131b937f1 \
--hash=sha256:c482e1246726616088532b5e964e39765b6d1520791348e6c9dc3af25b233828 \
--hash=sha256:ca637024ca67ab24a7fd6f65d280572c3794665eaf5edcc7e90a866544076878 \
--hash=sha256:e02d700ec8d9b1859790c0475df4e4092c7bf3272a4fd2c9f33d87fac4427b8f \
--hash=sha256:e5952d2d18b79f7dc25e62e014fe5a23eb1a3d2bc66318df8988a01b1a037c5b
mypy==1.11.2 \
--hash=sha256:06d26c277962f3fb50e13044674aa10553981ae514288cb7d0a738f495550b36 \
--hash=sha256:2ff93107f01968ed834f4256bc1fc4475e2fecf6c661260066a985b52741ddce \
--hash=sha256:36383a4fcbad95f2657642a07ba22ff797de26277158f1cc7bd234821468b1b6 \
--hash=sha256:37c7fa6121c1cdfcaac97ce3d3b5588e847aa79b580c1e922bb5d5d2902df19b \
--hash=sha256:3a66169b92452f72117e2da3a576087025449018afc2d8e9bfe5ffab865709ca \
--hash=sha256:3f14cd3d386ac4d05c5a39a51b84387403dadbd936e17cb35882134d4f8f0d24 \
--hash=sha256:41ea707d036a5307ac674ea172875f40c9d55c5394f888b168033177fce47383 \
--hash=sha256:478db5f5036817fe45adb7332d927daa62417159d49783041338921dcf646fc7 \
--hash=sha256:4a8a53bc3ffbd161b5b2a4fff2f0f1e23a33b0168f1c0778ec70e1a3d66deb86 \
--hash=sha256:539c570477a96a4e6fb718b8d5c3e0c0eba1f485df13f86d2970c91f0673148d \
--hash=sha256:57555a7715c0a34421013144a33d280e73c08df70f3a18a552938587ce9274f4 \
--hash=sha256:6e658bd2d20565ea86da7d91331b0eed6d2eee22dc031579e6297f3e12c758c8 \
--hash=sha256:6e7184632d89d677973a14d00ae4d03214c8bc301ceefcdaf5c474866814c987 \
--hash=sha256:75746e06d5fa1e91bfd5432448d00d34593b52e7e91a187d981d08d1f33d4385 \
--hash=sha256:7f9993ad3e0ffdc95c2a14b66dee63729f021968bff8ad911867579c65d13a79 \
--hash=sha256:801780c56d1cdb896eacd5619a83e427ce436d86a3bdf9112527f24a66618fef \
--hash=sha256:801ca29f43d5acce85f8e999b1e431fb479cb02d0e11deb7d2abb56bdaf24fd6 \
--hash=sha256:969ea3ef09617aff826885a22ece0ddef69d95852cdad2f60c8bb06bf1f71f70 \
--hash=sha256:a976775ab2256aadc6add633d44f100a2517d2388906ec4f13231fafbb0eccca \
--hash=sha256:af8d155170fcf87a2afb55b35dc1a0ac21df4431e7d96717621962e4b9192e70 \
--hash=sha256:b499bc07dbdcd3de92b0a8b29fdf592c111276f6a12fe29c30f6c417dd546d12 \
--hash=sha256:cd953f221ac1379050a8a646585a29574488974f79d8082cedef62744f0a0104 \
--hash=sha256:d42a6dd818ffce7be66cce644f1dff482f1d97c53ca70908dff0b9ddc120b77a \
--hash=sha256:e8960dbbbf36906c5c0b7f4fbf2f0c7ffb20f4898e6a879fcf56a41a08b0d318 \
--hash=sha256:edb91dded4df17eae4537668b23f0ff6baf3707683734b6a818d5b9d0c0c31a1 \
--hash=sha256:ee23de8530d99b6db0573c4ef4bd8f39a2a6f9b60655bf7a1357e585a3486f2b \
--hash=sha256:f7821776e5c4286b6a13138cc935e2e9b6fde05e081bdebf5cdb2bb97c9df81d
# via phantom-types (pyproject.toml)
mypy-extensions==1.0.0 \
--hash=sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d \
Expand Down

0 comments on commit 9f16a0a

Please sign in to comment.