Skip to content

Commit

Permalink
Consolidate Generic[...] parameter introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
nadove-ucsc committed Mar 17, 2023
1 parent 035e7ba commit 6173c7c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
8 changes: 2 additions & 6 deletions src/azul/indexer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
Optional,
Type,
TypeVar,
get_args,
)

import attr
from more_itertools import (
one,
)

from azul import (
reject,
Expand All @@ -35,6 +31,7 @@
MutableJSON,
MutableJSONs,
SupportsLessThan,
get_generic_type_params,
)
from azul.uuids import (
UUIDPartition,
Expand Down Expand Up @@ -350,8 +347,7 @@ def from_json(cls, ref: JSON) -> 'SourceRef':

@classmethod
def spec_cls(cls) -> Type[SourceSpec]:
base_cls = one(getattr(cls, '__orig_bases__'))
spec_cls, ref_cls = get_args(base_cls)
spec_cls, ref_cls = get_generic_type_params(cls, SourceSpec, SourceRef)
return spec_cls


Expand Down
13 changes: 2 additions & 11 deletions src/azul/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@
TypeVar,
TypedDict,
Union,
get_args,
)

import attr
from more_itertools import (
one,
)

from azul import (
CatalogName,
cached_property,
config,
require,
)
from azul.chalice import (
Authentication,
Expand All @@ -51,8 +46,6 @@
Bundle,
SOURCE_REF,
SOURCE_SPEC,
SourceRef,
SourceSpec,
SourcedBundleFQID,
)
from azul.indexer.document import (
Expand All @@ -65,6 +58,7 @@
JSON,
JSONs,
MutableJSON,
get_generic_type_params,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -446,10 +440,7 @@ def list_sources(self,
@cached_property
def _source_ref_cls(self) -> Type[SOURCE_REF]:
cls = type(self)
base_cls = one(getattr(cls, '__orig_bases__'))
spec_cls, ref_cls = get_args(base_cls)
require(issubclass(spec_cls, SourceSpec))
require(issubclass(ref_cls, SourceRef))
spec_cls, ref_cls = get_generic_type_params(cls)
assert ref_cls.spec_cls() is spec_cls
return ref_cls

Expand Down
11 changes: 11 additions & 0 deletions src/azul/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)
from typing import (
Any,
Generic,
Optional,
Protocol,
TYPE_CHECKING,
Expand All @@ -12,6 +13,8 @@
get_origin,
)

from more_itertools import one

PrimitiveJSON = Union[str, int, float, bool, None]

# Not every instance of Mapping or Sequence can be fed to json.dump() but those
Expand Down Expand Up @@ -158,6 +161,14 @@ def f(t):
return tuple(set(f(t)))


def get_generic_type_params(cls: type[Generic], *required_types: type):
base_cls = one(getattr(cls, '__orig_bases__'))
types = get_args(base_cls)
for required_type, type_ in zip(required_types, types):
assert issubclass(type_, required_type)
return types


# FIXME: Remove hacky import of SupportsLessThan
# https://github.com/DataBiosphere/azul/issues/2783
if TYPE_CHECKING:
Expand Down

0 comments on commit 6173c7c

Please sign in to comment.