Skip to content

Commit 6b98649

Browse files
authored
Refactor Members and Attrs to share code (#34)
1 parent 6d5ccca commit 6b98649

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

typemap/type_eval/_eval_operators.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
import re
88
import types
99
import typing
10-
from typing import _AnnotatedAlias as typing_AnnotatedAlias # type: ignore [attr-defined] # noqa: PLC2701
10+
from typing_extensions import _AnnotatedAlias as typing_AnnotatedAlias
1111

1212
from typemap import type_eval
1313
from typemap.type_eval import _apply_generic, _typing_inspect
1414
from typemap.type_eval._eval_typing import _eval_types
1515
from typemap.typing import (
1616
Attrs,
1717
Capitalize,
18+
DropAnnotations,
1819
FromUnion,
1920
GenericCallable,
21+
GetAnnotations,
2022
GetArg,
2123
GetArgs,
2224
GetAttr,
23-
GetAnnotations,
24-
DropAnnotations,
2525
IsSubSimilar,
2626
IsSubtype,
2727
Iter,
@@ -474,11 +474,8 @@ def _ann(x):
474474
return f
475475

476476

477-
@type_eval.register_evaluator(Attrs)
478-
@_lift_over_unions
479-
def _eval_Attrs(tp, *, ctx):
480-
hints = get_annotated_type_hints(tp, include_extras=True)
481-
477+
def _hints_to_members(hints, ctx):
478+
"""Convert a hints dictionary to a tuple of Member types."""
482479
return tuple[
483480
*[
484481
Member[
@@ -492,21 +489,21 @@ def _eval_Attrs(tp, *, ctx):
492489
]
493490

494491

492+
@type_eval.register_evaluator(Attrs)
493+
@_lift_over_unions
494+
def _eval_Attrs(tp, *, ctx):
495+
hints = get_annotated_type_hints(tp, include_extras=True)
496+
return _hints_to_members(hints, ctx)
497+
498+
495499
@type_eval.register_evaluator(Members)
496500
@_lift_over_unions
497501
def _eval_Members(tp, *, ctx):
498502
hints = {
499503
**get_annotated_type_hints(tp, include_extras=True),
500504
**get_annotated_method_hints(tp),
501505
}
502-
503-
attrs = [
504-
Member[
505-
typing.Literal[n], _eval_types(t, ctx), _mk_literal_union(*qs), d
506-
]
507-
for n, (t, qs, d) in hints.items()
508-
]
509-
return tuple[*attrs]
506+
return _hints_to_members(hints, ctx)
510507

511508

512509
##################################################################

typemap/type_eval/_eval_typing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import types
1111
import typing
1212

13-
from typing import _GenericAlias as typing_GenericAlias # type: ignore [attr-defined] # noqa: PLC2701
14-
from typing import _CallableGenericAlias as typing_CallableGenericAlias # type: ignore [attr-defined] # noqa: PLC2701
15-
from typing import _LiteralGenericAlias as typing_LiteralGenericAlias # type: ignore [attr-defined] # noqa: PLC2701
16-
from typing import _AnnotatedAlias as typing_AnnotatedAlias # type: ignore [attr-defined] # noqa: PLC2701
13+
from typing import ( # type: ignore [attr-defined] # noqa: PLC2701
14+
_GenericAlias as typing_GenericAlias,
15+
_CallableGenericAlias as typing_CallableGenericAlias,
16+
_LiteralGenericAlias as typing_LiteralGenericAlias,
17+
_AnnotatedAlias as typing_AnnotatedAlias,
18+
)
1719

1820

1921
if typing.TYPE_CHECKING:

0 commit comments

Comments
 (0)