Skip to content

gh-90527: Evaluate typing.ForwardRef inside typing.NewType #91637

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

uriyyo
Copy link
Member

@uriyyo uriyyo commented Apr 17, 2022

@JelleZijlstra
Copy link
Member

I think this will cause more trouble. NewTypes should generally be compared by identity, so creating a new one is problematic. I don't have a better idea though.

@uriyyo
Copy link
Member Author

uriyyo commented Apr 17, 2022

I think this will cause more trouble. NewTypes should generally be compared by identity, so creating a new one is problematic. I don't have a better idea though.

@JelleZijlstra I think we can mutate __supertype__ attr of NewType. So impl will look like this:

    if isinstance(t, NewType):
        arg = ForwardRef(t.__supertype__) if isinstance(t.__supertype__, str) else t.__supertype__
        t.__supertype__ = _eval_type(arg, globalns, localns, recursive_guard)
        return t

What do you think about such option?

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Apr 17, 2022

That's also problematic:

  1. It's nice to keep all typing objects immutable, since mutable state is the root of much evil.
  2. The globalns and localns in your code sample come from a specific call to get_type_hints. What if I call get_type_hints again later with a different namespace?

@uriyyo
Copy link
Member Author

uriyyo commented Apr 17, 2022

Possible solution can be add __eq__ implementation to typing.NewType. But it will go against identity compare.

@JelleZijlstra JelleZijlstra removed their request for review April 18, 2022 00:19
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.11 only security fixes label May 20, 2022
@AlexWaygood AlexWaygood added needs backport to 3.12 only security fixes and removed needs backport to 3.10 only security fixes labels Jun 6, 2023
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes and removed needs backport to 3.11 only security fixes labels May 9, 2024
@tomasr8 tomasr8 removed the needs backport to 3.12 only security fixes label Apr 10, 2025
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes stdlib Python modules in the Lib dir topic-typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants