Skip to content

Commit

Permalink
Remove __future__.annotations imports and manually stringize necessar…
Browse files Browse the repository at this point in the history
…y hints to standardize and bypass pydantic/pydantic#3401
  • Loading branch information
JacobHayes committed Nov 16, 2021
1 parent 86ecad7 commit 476a61b
Show file tree
Hide file tree
Showing 24 changed files with 24 additions and 72 deletions.
2 changes: 0 additions & 2 deletions 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
Expand Down
6 changes: 2 additions & 4 deletions 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
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/arti/backends/__init__.py
@@ -1,5 +1,3 @@
from __future__ import annotations

__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore


Expand Down
18 changes: 8 additions & 10 deletions 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
Expand Down Expand Up @@ -29,36 +27,36 @@ 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.
"""
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))

Expand All @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions 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
Expand Down
4 changes: 1 addition & 3 deletions 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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions 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
Expand Down Expand Up @@ -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_


Expand Down
2 changes: 0 additions & 2 deletions src/arti/internal/type_hints.py
@@ -1,5 +1,3 @@
from __future__ import annotations

import inspect
import sys
import types
Expand Down
6 changes: 2 additions & 4 deletions 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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions 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
Expand Down
10 changes: 4 additions & 6 deletions src/arti/partitions/__init__.py
@@ -1,5 +1,3 @@
from __future__ import annotations

__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore

import abc
Expand All @@ -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]]

Expand All @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions 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
Expand Down
2 changes: 0 additions & 2 deletions 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
Expand Down
4 changes: 1 addition & 3 deletions src/arti/storage/__init__.py
@@ -1,5 +1,3 @@
from __future__ import annotations

__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore

import abc
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/arti/storage/_internal.py
@@ -1,5 +1,3 @@
from __future__ import annotations

import string
from collections import defaultdict
from collections.abc import Mapping
Expand All @@ -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."
Expand Down
2 changes: 0 additions & 2 deletions 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
Expand Down
4 changes: 1 addition & 3 deletions 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
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 0 additions & 2 deletions src/arti/types/python.py
@@ -1,5 +1,3 @@
from __future__ import annotations

import datetime
from collections.abc import Mapping
from functools import partial
Expand Down
2 changes: 0 additions & 2 deletions src/arti/versions/__init__.py
@@ -1,5 +1,3 @@
from __future__ import annotations

__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore

import inspect
Expand Down
4 changes: 1 addition & 3 deletions 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
Expand All @@ -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]
Expand Down
2 changes: 0 additions & 2 deletions 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

Expand Down
2 changes: 0 additions & 2 deletions 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
Expand Down
4 changes: 1 addition & 3 deletions tests/arti/internal/test_utils.py
@@ -1,5 +1,3 @@
from __future__ import annotations

import math
import operator as op
import re
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 0 additions & 2 deletions tests/arti/views/test_views.py
@@ -1,5 +1,3 @@
from __future__ import annotations

from typing import ClassVar

import pytest
Expand Down

0 comments on commit 476a61b

Please sign in to comment.