-
Notifications
You must be signed in to change notification settings - Fork 5
Added GenericAliasHandler class [ENG-226] #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -172,6 +172,28 @@ def handle(self, obj: Any, hasher: "SemanticHasherProtocol") -> Any: | |||
| return f"type:{module}.{qualname}" | ||||
|
|
||||
|
|
||||
| class GenericAliasHandler: | ||||
| """ | ||||
| Handler for generic alias type annotations such as ``dict[int, list[int]]`` | ||||
| (``types.GenericAlias``) and ``typing`` generics (``typing._GenericAlias``). | ||||
|
|
||||
| Produces a stable dict containing the origin type and a list of hashed | ||||
| argument types so that structurally identical generic annotations always | ||||
| yield the same hash, and structurally different ones yield different hashes. | ||||
| """ | ||||
|
|
||||
| def handle(self, obj: Any, hasher: "SemanticHasherProtocol") -> Any: | ||||
| origin = getattr(obj, "__origin__", None) | ||||
| args = getattr(obj, "__args__", None) or () | ||||
| if origin is None: | ||||
| return f"generic_alias:{obj!r}" | ||||
| return { | ||||
| "__type__": "generic_alias", | ||||
| "origin": hasher.hash_object(origin).to_string(), | ||||
| "args": [hasher.hash_object(arg).to_string() for arg in args], | ||||
| } | ||||
|
Comment on lines
+175
to
+194
|
||||
|
|
||||
|
|
||||
| class ArrowTableHandler: | ||||
| """ | ||||
| Handler for ``pa.Table`` and ``pa.RecordBatch`` objects. | ||||
|
|
@@ -321,6 +343,19 @@ def register_builtin_handlers( | |||
| # type objects (classes used as values, e.g. passed in a dict) | ||||
| registry.register(type, TypeObjectHandler()) | ||||
|
|
||||
| # generic alias type annotations: dict[int, str], list[str], etc. | ||||
| import types as _types | ||||
|
||||
| import types as _types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type_handler_registryin this context only registerstypes.GenericAlias, but the PR description (andregister_builtin_handlers) also registerstyping._GenericAlias. Because the defaultsemantic_hasherhere uses this JSON-constructed registry,typinggenerics liketyping.Dict[int, str]/typing.Optional[int]can still raise in strict mode. Consider adding atyping._GenericAliasentry (or switching this context to a registry implementation that conditionally registers it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brian-arnold this is a good point. Shall we register _GenericAlias as well?