Skip to content

TypedDict and NamedTuple do not evaluate cross-module ForwardRef in all cases #90531

Description

@aha79
mannequin
BPO 46373
Nosy @gvanrossum, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @kumaraditya303, @AlexWaygood, @aha79

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-01-14.09:06:20.374>
labels = ['type-bug', 'library', '3.10', '3.11']
title = 'TypedDict and NamedTuple do not evaluate cross-module ForwardRef in all cases'
updated_at = <Date 2022-01-14.09:06:20.374>
user = 'https://github.com/aha79'

bugs.python.org fields:

activity = <Date 2022-01-14.09:06:20.374>
actor = 'andreash'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2022-01-14.09:06:20.374>
creator = 'andreash'
dependencies = []
files = []
hgrepos = []
issue_num = 46373
keywords = []
message_count = 1.0
messages = ['410547']
nosy_count = 7.0
nosy_names = ['gvanrossum', 'JelleZijlstra', 'sobolevn', 'kj', 'kumaraditya', 'AlexWaygood', 'andreash']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue46373'
versions = ['Python 3.10', 'Python 3.11']

Activity

  1. aha79 commented on Jan 14, 2022

    aha79mannequin
    MannequinAuthor

    TypedDict does not resolve cross-module ForwardRefs when the ForwardRef is not a direct one.

    In other words the fix #71204 (bpo-41249) for TypedDict seems incomplete.

    The same issue seem to exist for NamedTuple.

    Example:

       #module.py
       TD = typing.TypedDict("TD", {'test': typing.List[typing.Optional['Y']]})
       class Y:
          pass
    
    
       # other module
       class TDSub(module.TD):
           a: int
       get_type_hints(TDSub)
       # -> Exception   NameError: Y not found

    On the other hand, with direct ForwardRef, as e.g. in
    TD = typing.TypedDict("TD", {'test': 'Y' } )

    it works (that was indeed fixed by #71204)

    Same issue exists for NamedTuple. There neither of the above works, i.e. cross-module ForwardRefs are never resolve (but they could - als NamedTuple has the __module__ member set with to calling module). I am not sure if inheritance for NamedTuple is supported so I do not know if it is really a bug.

    The problem in the code is that in TypedDict the module parameter is passed only onto the immediate ForwardRef. One option could be to recursively walk the type and search for unpatched ForwardRefs and set the module parameter.
    On the other hand, the retroactive patching of forward refs is problematic as it may mess with the caching mechanism im typing.py. There may be a type with ForwardRef already in cache (and used by more than one user), but only for one user the module is to be updated. So probably a correct implementation is tricky, or some other way has to be found to update the module of ForwardRefs (e.g. by copying the type tree).

    For NamedTuple the whole mechanism of passing the module parameter to the ForwardRefs is not done (not even for direct ForwardRef ones).

    Not sure how important this (likely not very) is as I do not use TypedDict and NamedTuple. This is just to report it.

  2. added
    3.11only security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Jan 14, 2022
  3. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions