Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_attrs_type_checking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import TYPE_CHECKING, Literal

from typemap.type_eval import eval_typing
import typemap_extensions as typing

if TYPE_CHECKING:

class HiddenNamespace:
pass


class Base:
def rebuild(self, ns: HiddenNamespace | None = None) -> bool | None:
return None


class Model(Base):
name: str


def test_attrs_does_not_evaluate_method_annotations() -> None:
attrs = eval_typing(typing.Attrs[Model])

assert len(attrs.__args__) == 1
assert eval_typing(attrs.__args__[0].name) == Literal["name"]
assert eval_typing(attrs.__args__[0].type) is str
4 changes: 3 additions & 1 deletion typemap/type_eval/_apply_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ def _resolved_function_signature(func, args):

def get_local_defns(
boxed: Boxed,
*,
include_methods: bool = True,
) -> tuple[
dict[str, Any],
dict[
Expand All @@ -343,7 +345,7 @@ def get_local_defns(

stuff = inspect.unwrap(orig)

if isinstance(stuff, types.FunctionType):
if include_methods and isinstance(stuff, types.FunctionType):
local_fn: Any = None

# TODO: This annos_ok thing is a hack because processing
Expand Down
4 changes: 3 additions & 1 deletion typemap/type_eval/_eval_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def get_annotated_type_hints(cls, *, ctx, attrs_only=False, **kwargs):
for abox in reversed(box.mro):
acls = abox.alias_type()

annos, _ = _apply_generic.get_local_defns(abox)
annos, _ = _apply_generic.get_local_defns(
abox, include_methods=not attrs_only
)
for k, ty in annos.items():
quals = set()

Expand Down
Loading