Skip to content

Add GetSpecialAttr.#61

Merged
dnwpark merged 4 commits intomainfrom
typename
Jan 30, 2026
Merged

Add GetSpecialAttr.#61
dnwpark merged 4 commits intomainfrom
typename

Conversation

@dnwpark
Copy link
Contributor

@dnwpark dnwpark commented Jan 29, 2026

Allows getting the __name__, __module__, __qualname__ special attributes.

    d = eval_typing(GetSpecialAttr[int, Literal["__name__"]])
    assert d == Literal["int"]
    d = eval_typing(GetSpecialAttr[list[int], Literal["__name__"]])
    assert d == Literal["list"]

    d = eval_typing(GetSpecialAttr[C, Literal["__name__"]])
    assert d == Literal["C"]
    d = eval_typing(GetSpecialAttr[C, Literal["__module__"]])
    assert d == Literal["tests.test_type_eval"]
    d = eval_typing(GetSpecialAttr[C, Literal["__qualname__"]])
    assert d == Literal["test_eval_typename_03.<locals>.C"]

@dnwpark dnwpark requested a review from msullivan January 29, 2026 18:02
@vercel
Copy link

vercel bot commented Jan 29, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
python-typemap Ready Ready Preview, Comment Jan 30, 2026 10:09pm

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems plausible, but if we are doing this, we probably also need to expose __module__ (and maybe __qualname__?).

And adding one operator for this is probably OK but multiple ones seems to be stretching it.

Would it be too much of a hack to define GetAttr to work on __name__, __module__, and __qualname__ without putting those fields in Members?

If it is, then maybe we could add a GetMetadataAttr that supports stuff like __name__.

Thoughts?

@dnwpark dnwpark changed the title Add TypeName. Add GetSpecialAttr. Jan 29, 2026
@dnwpark dnwpark changed the base branch from main to not-pre January 29, 2026 19:56
Base automatically changed from not-pre to main January 29, 2026 20:06
Comment on lines +929 to +932
raise TypeError(
f"Invalid type argument to GetSpecialAttr: "
f"{attr} must be one of __name__, __module__, or __qualname__"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should return Never

@dnwpark dnwpark merged commit 794a4ec into main Jan 30, 2026
5 checks passed
@dnwpark dnwpark deleted the typename branch January 30, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants