-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
type aliases: support generics (#618)
- Loading branch information
Showing
9 changed files
with
130 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Utilities for type aliases.""" | ||
|
||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from ._compat import is_generic | ||
from ._generics import deep_copy_with | ||
from .dispatch import StructureHook | ||
from .gen._generics import generate_mapping | ||
|
||
if TYPE_CHECKING: | ||
from .converters import BaseConverter | ||
|
||
__all__ = ["is_type_alias", "get_type_alias_base", "type_alias_structure_factory"] | ||
|
||
if sys.version_info >= (3, 12): | ||
from types import GenericAlias | ||
from typing import TypeAliasType | ||
|
||
def is_type_alias(type: Any) -> bool: | ||
"""Is this a PEP 695 type alias?""" | ||
return isinstance( | ||
type.__origin__ if type.__class__ is GenericAlias else type, TypeAliasType | ||
) | ||
|
||
else: | ||
|
||
def is_type_alias(type: Any) -> bool: | ||
"""Is this a PEP 695 type alias?""" | ||
return False | ||
|
||
|
||
def get_type_alias_base(type: Any) -> Any: | ||
""" | ||
What is this a type alias of? | ||
Works only on 3.12+. | ||
""" | ||
return type.__value__ | ||
|
||
|
||
def type_alias_structure_factory(type: Any, converter: BaseConverter) -> StructureHook: | ||
base = get_type_alias_base(type) | ||
if is_generic(type): | ||
mapping = generate_mapping(type) | ||
if base.__name__ in mapping: | ||
# Probably just type T = T | ||
base = mapping[base.__name__] | ||
else: | ||
base = deep_copy_with(base, mapping) | ||
res = converter.get_structure_hook(base) | ||
if res == converter._structure_call: | ||
# we need to replace the type arg of `structure_call` | ||
return lambda v, _, __base=base: __base(v) | ||
return lambda v, _, __base=base: res(v, __base) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.