From 476a61bd788008f0caee1cbd6a8b5873624c82a3 Mon Sep 17 00:00:00 2001 From: Jacob Hayes Date: Tue, 16 Nov 2021 15:14:59 -0500 Subject: [PATCH] Remove __future__.annotations imports and manually stringize necessary hints to standardize and bypass samuelcolvin/pydantic#3401 --- src/arti/annotations/__init__.py | 2 -- src/arti/artifacts/__init__.py | 6 ++---- src/arti/backends/__init__.py | 2 -- src/arti/fingerprints/__init__.py | 18 ++++++++---------- src/arti/formats/__init__.py | 2 -- src/arti/graphs/__init__.py | 4 +--- src/arti/internal/models.py | 6 ++---- src/arti/internal/type_hints.py | 2 -- src/arti/internal/utils.py | 6 ++---- src/arti/io/__init__.py | 2 -- src/arti/partitions/__init__.py | 10 ++++------ src/arti/producers/__init__.py | 2 -- src/arti/statistics/__init__.py | 2 -- src/arti/storage/__init__.py | 4 +--- src/arti/storage/_internal.py | 4 +--- src/arti/thresholds/__init__.py | 2 -- src/arti/types/__init__.py | 4 +--- src/arti/types/python.py | 2 -- src/arti/versions/__init__.py | 2 -- src/arti/views/__init__.py | 4 +--- stubs/parse.pyi | 2 -- stubs/sgqlc/types/__init__.pyi | 2 -- tests/arti/internal/test_utils.py | 4 +--- tests/arti/views/test_views.py | 2 -- 24 files changed, 24 insertions(+), 72 deletions(-) diff --git a/src/arti/annotations/__init__.py b/src/arti/annotations/__init__.py index f83b2d5c..6b62bc1a 100644 --- a/src/arti/annotations/__init__.py +++ b/src/arti/annotations/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from arti.internal.models import Model diff --git a/src/arti/artifacts/__init__.py b/src/arti/artifacts/__init__.py index 8b1b1460..67efb850 100644 --- a/src/arti/artifacts/__init__.py +++ b/src/arti/artifacts/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from itertools import chain @@ -42,7 +40,7 @@ class BaseArtifact(Model): storage: Storage[Any] # Hide the producer to prevent showing the entire upstream graph - producer: Optional[Producer] = Field(None, repr=False) + producer: Optional["Producer"] = Field(None, repr=False) # TODO: Allow smarter type/format/storage merging w/ the default? @@ -94,7 +92,7 @@ def _merge_class_defaults(cls, value: tuple[Any, ...], field: ModelField) -> tup return tuple(chain(cls.__fields__[field.name].default, value)) @classmethod - def cast(cls, value: Any) -> Artifact: + def cast(cls, value: Any) -> "Artifact": """Attempt to convert an arbitrary value to an appropriate Artifact instance. `Artifact.cast` is used to convert values assigned to an `Artifact.box` (such as diff --git a/src/arti/backends/__init__.py b/src/arti/backends/__init__.py index cd2268e6..95b2a0e1 100644 --- a/src/arti/backends/__init__.py +++ b/src/arti/backends/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore diff --git a/src/arti/fingerprints/__init__.py b/src/arti/fingerprints/__init__.py index fece80c0..260ceaca 100644 --- a/src/arti/fingerprints/__init__.py +++ b/src/arti/fingerprints/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from functools import reduce @@ -29,24 +27,24 @@ class Fingerprint(Model): key: Optional[int64] - def combine(self, *others: Fingerprint) -> Fingerprint: + def combine(self, *others: "Fingerprint") -> "Fingerprint": return reduce(xor, others, self) @classmethod - def empty(cls) -> Fingerprint: + def empty(cls) -> "Fingerprint": """Return a Fingerprint that, when combined, will return Fingerprint.empty()""" return cls(key=None) @classmethod - def from_int(cls, x: int, /) -> Fingerprint: + def from_int(cls, x: int, /) -> "Fingerprint": return cls.from_int64(int64(x)) @classmethod - def from_int64(cls, x: int64, /) -> Fingerprint: + def from_int64(cls, x: int64, /) -> "Fingerprint": return cls(key=x) @classmethod - def from_string(cls, x: str, /) -> Fingerprint: + def from_string(cls, x: str, /) -> "Fingerprint": """Fingerprint an arbitrary string. Fingerprints using Farmhash Fingerprint64, converted to int64 via two's complement. @@ -54,11 +52,11 @@ def from_string(cls, x: str, /) -> Fingerprint: return cls.from_uint64(uint64(farmhash.fingerprint64(x))) @classmethod - def from_uint64(cls, x: uint64, /) -> Fingerprint: + def from_uint64(cls, x: uint64, /) -> "Fingerprint": return cls.from_int64(int64(x)) @classmethod - def identity(cls) -> Fingerprint: + def identity(cls) -> "Fingerprint": """Return a Fingerprint that, when combined, will return the other Fingerprint.""" return cls(key=int64(0)) @@ -70,7 +68,7 @@ def is_empty(self) -> bool: def is_identity(self) -> bool: return self.key == 0 - def __xor__(self, other: Fingerprint) -> Fingerprint: + def __xor__(self, other: "Fingerprint") -> "Fingerprint": if self.key is None: return Fingerprint.empty() if isinstance(other, Fingerprint): diff --git a/src/arti/formats/__init__.py b/src/arti/formats/__init__.py index 8c3885ac..1e3cb304 100644 --- a/src/arti/formats/__init__.py +++ b/src/arti/formats/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from typing import ClassVar diff --git a/src/arti/graphs/__init__.py b/src/arti/graphs/__init__.py index e91e90da..0e688d55 100644 --- a/src/arti/graphs/__init__.py +++ b/src/arti/graphs/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from types import TracebackType @@ -33,7 +31,7 @@ def __init__(self, name: str, *, backend: Optional[Backend] = None) -> None: # Seal the class and convert the Boxes self._toggle(sealed=True) - def __enter__(self) -> Graph: + def __enter__(self) -> "Graph": self._toggle(sealed=False) return self diff --git a/src/arti/internal/models.py b/src/arti/internal/models.py index 15cbadf1..d8f5621b 100644 --- a/src/arti/internal/models.py +++ b/src/arti/internal/models.py @@ -1,5 +1,3 @@ -from __future__ import annotations - from collections.abc import Mapping, Sequence from functools import cached_property from typing import Annotated, Any, ClassVar, Literal, Optional, get_args, get_origin @@ -145,8 +143,8 @@ class Config: @classmethod def _pydantic_type_system_post_field_conversion_hook_( - cls, type_: arti.types.Type, *, name: str, required: bool - ) -> arti.types.Type: + cls, type_: "arti.types.Type", *, name: str, required: bool + ) -> "arti.types.Type": return type_ diff --git a/src/arti/internal/type_hints.py b/src/arti/internal/type_hints.py index 9812e532..96390078 100644 --- a/src/arti/internal/type_hints.py +++ b/src/arti/internal/type_hints.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import inspect import sys import types diff --git a/src/arti/internal/utils.py b/src/arti/internal/utils.py index 5645d00a..1cecd7f5 100644 --- a/src/arti/internal/utils.py +++ b/src/arti/internal/utils.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import inspect import os.path from collections.abc import Callable, Generator, MutableMapping @@ -204,7 +202,7 @@ def __xor__(self: _int_sub, n: int) -> _int_sub: class int64(_int): _min, _max = -(2 ** 63), (2 ** 63) - 1 - def __new__(cls, i: Union[int, int64, uint64]) -> int64: + def __new__(cls, i: Union[int, "int64", "uint64"]) -> "int64": if i > cls._max: if isinstance(i, uint64): i = int(i) - uint64._max - 1 @@ -218,7 +216,7 @@ def __new__(cls, i: Union[int, int64, uint64]) -> int64: class uint64(_int): _min, _max = 0, (2 ** 64) - 1 - def __new__(cls, i: Union[int, int64, uint64]) -> uint64: + def __new__(cls, i: Union[int, int64, "uint64"]) -> "uint64": if i > cls._max: raise ValueError(f"{i} is too large for uint64.") if i < cls._min: diff --git a/src/arti/io/__init__.py b/src/arti/io/__init__.py index b1357d57..0b6462b0 100644 --- a/src/arti/io/__init__.py +++ b/src/arti/io/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from typing import Any diff --git a/src/arti/partitions/__init__.py b/src/arti/partitions/__init__.py index 493a8460..57862c21 100644 --- a/src/arti/partitions/__init__.py +++ b/src/arti/partitions/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore import abc @@ -18,7 +16,7 @@ class key_component(property): class PartitionKey(Model): _abstract_ = True - _by_type_: ClassVar[dict[type[Type], type[PartitionKey]]] = {} + _by_type_: ClassVar[dict[type[Type], type["PartitionKey"]]] = {} matching_type: ClassVar[type[Type]] @@ -40,15 +38,15 @@ def key_components(cls) -> frozenset[str]: @classmethod @abc.abstractmethod - def from_key_components(cls, **key_components: str) -> PartitionKey: + def from_key_components(cls, **key_components: str) -> "PartitionKey": raise NotImplementedError(f"Unable to parse '{cls.__name__}' from: {key_components}") @classmethod - def key_type_for(cls, type_: Type) -> type[PartitionKey]: + def key_type_for(cls, type_: Type) -> type["PartitionKey"]: return cls._by_type_[type(type_)] @classmethod - def key_types_from(cls, type_: Type) -> frozendict[str, type[PartitionKey]]: + def key_types_from(cls, type_: Type) -> frozendict[str, type["PartitionKey"]]: if not isinstance(type_, Struct): return frozendict() return frozendict( diff --git a/src/arti/producers/__init__.py b/src/arti/producers/__init__.py index 3fcb2c7a..9631a080 100644 --- a/src/arti/producers/__init__.py +++ b/src/arti/producers/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from collections.abc import Callable, Iterable, Iterator diff --git a/src/arti/statistics/__init__.py b/src/arti/statistics/__init__.py index b1615df7..26165446 100644 --- a/src/arti/statistics/__init__.py +++ b/src/arti/statistics/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from arti.artifacts import Statistic as Statistic # noqa: F401 diff --git a/src/arti/storage/__init__.py b/src/arti/storage/__init__.py index 65ad4837..699ca595 100644 --- a/src/arti/storage/__init__.py +++ b/src/arti/storage/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore import abc @@ -17,7 +15,7 @@ class StoragePartition(Model): keys: CompositeKey fingerprint: Optional[Fingerprint] = None - def with_fingerprint(self) -> StoragePartition: + def with_fingerprint(self) -> "StoragePartition": return self.copy(update={"fingerprint": self.compute_fingerprint()}) @abc.abstractmethod diff --git a/src/arti/storage/_internal.py b/src/arti/storage/_internal.py index 6a91a619..67ef49eb 100644 --- a/src/arti/storage/_internal.py +++ b/src/arti/storage/_internal.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import string from collections import defaultdict from collections.abc import Mapping @@ -25,7 +23,7 @@ def _err_if_no_attribute(self) -> None: f"'{self._name}' cannot be used directly in a partition path; access one of the key components (eg: '{self._name}.{example}')." ) - def __getattr__(self, name: str) -> WildcardPlaceholder: + def __getattr__(self, name: str) -> "WildcardPlaceholder": if self._attribute is not None: raise ValueError( f"'{self._name}.{self._attribute}.{name}' cannot be used in a partition path; only immediate '{self._name}' attributes (such as '{self._attribute}') can be used." diff --git a/src/arti/thresholds/__init__.py b/src/arti/thresholds/__init__.py index 8e98fab6..345ca807 100644 --- a/src/arti/thresholds/__init__.py +++ b/src/arti/thresholds/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from typing import Any, ClassVar diff --git a/src/arti/types/__init__.py b/src/arti/types/__init__.py index ba34e106..9d544f9d 100644 --- a/src/arti/types/__init__.py +++ b/src/arti/types/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from collections.abc import Iterable, Iterator, Mapping @@ -266,7 +264,7 @@ def generate( artigraph: type[Type], system: Any, priority: int = 0, - type_system: TypeSystem, + type_system: "TypeSystem", name: Optional[str] = None, ) -> type[TypeAdapter]: """Generate a _ScalarClassTypeAdapter subclass for the scalar system type.""" diff --git a/src/arti/types/python.py b/src/arti/types/python.py index ae2aa6a1..03fb6162 100644 --- a/src/arti/types/python.py +++ b/src/arti/types/python.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import datetime from collections.abc import Mapping from functools import partial diff --git a/src/arti/versions/__init__.py b/src/arti/versions/__init__.py index cbab868e..5aeed00f 100644 --- a/src/arti/versions/__init__.py +++ b/src/arti/versions/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore import inspect diff --git a/src/arti/views/__init__.py b/src/arti/views/__init__.py index 3da26fd6..aa90ba5c 100644 --- a/src/arti/views/__init__.py +++ b/src/arti/views/__init__.py @@ -1,5 +1,3 @@ -from __future__ import annotations - __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore from typing import Any, ClassVar @@ -16,7 +14,7 @@ class View(Model): """ _abstract_ = True - _registry_: ClassVar[dict[type, type[View]]] = {} + _registry_: ClassVar[dict[type, type["View"]]] = {} priority: ClassVar[int] = 0 # Set priority of this view for its python_type. Higher is better. python_type: ClassVar[type] diff --git a/stubs/parse.pyi b/stubs/parse.pyi index 2662402a..78ecd262 100644 --- a/stubs/parse.pyi +++ b/stubs/parse.pyi @@ -1,7 +1,5 @@ # NOTE: These stubs are partial -from __future__ import annotations - import re from typing import Literal, Optional, Union, overload diff --git a/stubs/sgqlc/types/__init__.pyi b/stubs/sgqlc/types/__init__.pyi index 042c6850..0482b64d 100644 --- a/stubs/sgqlc/types/__init__.pyi +++ b/stubs/sgqlc/types/__init__.pyi @@ -1,8 +1,6 @@ # pylint: skip-file # flake8: noqa -from __future__ import annotations - from collections import OrderedDict from collections.abc import Iterator from typing import Any, Callable, Literal, Optional, TypeVar diff --git a/tests/arti/internal/test_utils.py b/tests/arti/internal/test_utils.py index 55df18f3..ddfbedec 100644 --- a/tests/arti/internal/test_utils.py +++ b/tests/arti/internal/test_utils.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import math import operator as op import re @@ -314,7 +312,7 @@ def test_TypedBox_bad_hint() -> None: def test_TypedBox_cast() -> None: class Coord(BaseCoord): @classmethod - def cast(cls, value: tuple[int, int]) -> Coord: + def cast(cls, value: tuple[int, int]) -> "Coord": return cls(*value) CoordBox = TypedBox[str, Coord] diff --git a/tests/arti/views/test_views.py b/tests/arti/views/test_views.py index c42fef79..fddd6908 100644 --- a/tests/arti/views/test_views.py +++ b/tests/arti/views/test_views.py @@ -1,5 +1,3 @@ -from __future__ import annotations - from typing import ClassVar import pytest