GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — get_type_hints fails if there are un-annotated fields in a dataclass · Issue #82129 · python/cpython

get_type_hints fails if there are un-annotated fields in a dataclass #82129

New issue
Copy link
New issue
Copy link
Closed
#133406
Closed
get_type_hints fails if there are un-annotated fields in a dataclass#82129
#133406
Copy link
Assignees
ericvsmith
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifetopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

a-recknagel
mannequin
opened on Aug 26, 2019
Issue body actions
BPO 37948
Nosy @ericvsmith, @serhiy-storchaka, @ilevkivskyi, @a-recknagel

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 = 'https://github.com/ericvsmith'
closed_at = None
created_at = <Date 2019-08-26.06:29:12.163>
labels = ['3.8', 'type-bug', '3.7']
title = 'get_type_hints fails if there are un-annotated fields in a dataclass'
updated_at = <Date 2020-03-08.13:51:23.330>
user = 'https://github.com/a-recknagel'

bugs.python.org fields:

activity = <Date 2020-03-08.13:51:23.330>
actor = 'serhiy.storchaka'
assignee = 'eric.smith'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2019-08-26.06:29:12.163>
creator = 'arne'
dependencies = []
files = []
hgrepos = []
issue_num = 37948
keywords = []
message_count = 5.0
messages = ['350488', '350973', '355559', '356829', '363662']
nosy_count = 4.0
nosy_names = ['eric.smith', 'serhiy.storchaka', 'levkivskyi', 'arne']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue37948'
versions = ['Python 3.7', 'Python 3.8']

Linked PRs

  • gh-82129: Fix NameError on get_type_hints in dataclasses #122232
  • gh-82129: Provide __annotate__ method for dataclasses from make_dataclass #122262
  • gh-82129: Improve annotations for make_dataclass() #133406

Activity

  1. a-recknagel commented on Aug 26, 2019

    @a-recknagel
    a-recknagelmannequin
    on Aug 26, 2019
    MannequinAuthor
    More actions

    When declaring a dataclass with make_dataclass, it is valid to omit type information for fields. __annotations__ understands it and just adds typing.Any, but typing.get_type_hints fails with a cryptic error message:

    >>> import dataclasses
    >>> import typing
    >>> A = dataclasses.make_dataclass('A', ['a_var'])
    >>> A.__annotations__
    {'a_var': 'typing.Any'}
    >>> typing.get_type_hints(A)
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 973, in get_type_hints
        value = _eval_type(value, base_globals, localns)
      File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 260, in _eval_type
        return t._evaluate(globalns, localns)
      File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 464, in _evaluate
        eval(self.__forward_code__, globalns, localns),
      File "<string>", line 1, in <module>
    NameError: name 'typing' is not defined

    Adding typing.Any explicitly is an obvious workaround:

    >>> B = dataclasses.make_dataclass('B', [('a_var', typing.Any)])
    >>> typing.get_type_hints(B)
    {'a_var': typing.Any}

    There is already a bug filed regarding datalcasses and get_type_hints which might be related: https://bugs.python.org/issue34776

  2. a-recknagel
    added
    3.7 (EOL)end of lifeend of life
    3.8 (EOL)end of lifeend of life
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 26, 2019
  3. ericvsmith
    self-assigned this
    on Aug 26, 2019
  4. ilevkivskyi commented on Sep 2, 2019

    @ilevkivskyi
    ilevkivskyi
    on Sep 2, 2019
    Member
    More actions

    It looks like #9518 will fix also this one.

  5. ericvsmith commented on Oct 28, 2019

    @ericvsmith
    ericvsmith
    on Oct 28, 2019
    Member
    More actions

    I'm not sure what can be done with this. The problem is that the decorator doesn't know what's in the caller's namespace. The type being added is "typing.Any". If the caller doesn't import typing, then get_type_hints will fail (as demonstrated here).

    The only thing I can think of is using a type that's in builtins. "object" springs to mine, but of course that's semantically incorrect.

    Or, maybe I could use "dataclasses.sys.modules['typing'].Any". I don't currently import sys (I don't think), but this should be a cheap import. Then if typing.get_type_hints() is called, we know typing will have already been importing.

    But what if "dataclasses" isn't in the caller's namespace? I guess if I could find some way to navigate to sys.modules from __builtins__, that would largely work, absent playing games with builtins.

  6. ilevkivskyi commented on Nov 17, 2019

    @ilevkivskyi
    ilevkivskyi
    on Nov 17, 2019
    Member
    More actions

    I'm not sure what can be done with this. The problem is that the decorator doesn't know what's in the caller's namespace. The type being added is "typing.Any". If the caller doesn't import typing, then get_type_hints will fail (as demonstrated here).

    IIUC the main problem is that get_type_hints() fails even if typing is imported. I would expect this to work (just repeating the original example in a more compact form):

    import dataclasses
    import typing
    A = dataclasses.make_dataclass('A', ['a_var'])
    typing.get_type_hints(A)  # This currently crashes

    Interestingly, if I use a very similar call that it works:

    >>> typing.get_type_hints(A, globalns=globals())
    {'a_var': typing.Any}

    So the core of the issue is that the globals are identified incorrectly, and indeed if I look at the generated class it looks wrong:

    >>> A.__module__
    'types'  # Should be '__main__'

    I think we should fix the __module__ attribute of the dynamically generated dataclasses (for example the way it is done for named tuples).

    Btw, #14166 may potentially fix the __module__ attribute here too.

  7. serhiy-storchaka commented on Mar 8, 2020

    @serhiy-storchaka
    serhiy-storchaka
    on Mar 8, 2020
    Member
    More actions

    PR 14166 does not fix this issue.

  8. ezio-melotti
    transferred this issue fromon Apr 10, 2022
  9. AlexWaygood
    added
    topic-typing
    on Apr 13, 2022
  10. AlexWaygood commented on Apr 14, 2022

    @AlexWaygood
    AlexWaygood
    on Apr 14, 2022
    Member
    More actions

    Reproduced on 3.11.07a

  11. AlexWaygood
    added
    3.11only security fixesonly security fixes
    3.10 (EOL)end of lifeend of life
    3.9 (EOL)end of lifeend of life
    and removed
    3.8 (EOL)end of lifeend of life
    3.7 (EOL)end of lifeend of life
    on Apr 14, 2022
  12. DavidCEllis commented on Jul 24, 2024

    @DavidCEllis
    DavidCEllis
    on Jul 24, 2024
    Contributor
    More actions

    I ran into this when trying to make a quick dataclass to demo cattrs code generation.

    import dataclasses
    import cattrs
    A = dataclasses.make_dataclass('A', ['a_var'])
    a = A("a_value")
    cattrs.unstructure(a)
    NameError: name 'typing' is not defined. Did you forget to import 'typing'?
    

    Initially I thought this was a cattrs bug, but on looking at the trace it uses get_type_hints so it's related to this same issue.

    In 3.13.0b4, 3.12, 3.11 (and earlier) this still reproduces with:

    import dataclasses
    from typing import get_type_hints
    A = dataclasses.make_dataclass('A', ['a_var'])
    print(get_type_hints(A))

    However, in 3.13.0b4, and 3.12 (but not 3.11 or earlier) if you plainly import typing this now "works" - I think because __module__ is now set when it was not in 3.11:

    import dataclasses
    import typing
    A = dataclasses.make_dataclass('A', ['a_var'])
    print(typing.get_type_hints(A))

    I say "works" because it's evaluating the name 'typing' from the script file that defines the dataclass. Unlikely someone would do this, but I'd still consider this incorrect behaviour.

    import dataclasses
    from typing import get_type_hints
    class typing:
        Any = int
    
    A = dataclasses.make_dataclass('A', ['a_var'])
    print(get_type_hints(A))
    {'a_var': <class 'int'>}
    

    It also fails if A is defined in a file that doesn't import typing and typing.get_type_hints is used from a separate file. Likewise if inspect.get_annotations(..., eval_str=True) is used you get the same error without typing needing to be imported anywhere.

    Some possible solutions:

    • Use a deferred import of typing only if someone is using make_dataclass with untyped fields and put in the actual typing.Any object.
    • Track and remove the untyped fields from __annotations__ after the dataclass has been created
      • I think this is correct, but it might break code if it makes the assumption that a field existing means it's in __annotations__
    • Put a dict-like object in __annotations__ that will import and return typing.Any for untyped fields on demand.
      • This is kind of fiddly and adds a whole class just for a presumably lightly used feature
  13. sobolevn commented on Jul 24, 2024

    @sobolevn
    sobolevn
    on Jul 24, 2024
    Member
    More actions

    Put a dict-like object in annotations that will import and return typing.Any for untyped fields on demand.

    I think that this is a proper solution. This will be fixed after __annotate__ will be fully supported.

  14. DavidCEllis commented on Jul 24, 2024

    @DavidCEllis
    DavidCEllis
    on Jul 24, 2024
    Contributor
    More actions

    If you want to avoid importing typing for the creation of such a class you still have to temporarily put something else in place for dataclasses to use in construction, and then replace it with something that will correctly evaluate afterwards. Otherwise dataclasses will trigger the imports when it inspects the annotations to create the class in the first place.

    PEP649/749 could change things, but can/should this also be fixed for 3.13/3.12?

  15. sobolevn
    added 2 commits that reference this issue on Jul 24, 2024

    pythongh-82129: Fix `NameError` on `get_type_hints` in `dataclasses`

    a96efef

    pythongh-82129: Fix `NameError` on `get_type_hints` in `dataclasses`

    0d10812
  16. JelleZijlstra commented on May 4, 2025

    @JelleZijlstra
    JelleZijlstra
    on May 4, 2025
    Member
    More actions

    The original example no longer fails:

    >>> import dataclasses, typing
    >>> A = dataclasses.make_dataclass('A', ['a_var'])
    ... 
    >>> A.__annotations__
    {'a_var': 'typing.Any'}
    >>> typing.get_type_hints(A)
    ... 
    {'a_var': typing.Any}
    

    We can still improve the behavior a bit, I'll send a patch on top of #122262.

    Edit: Just reread David Ellis's comment above and this actually only works by accident.

  17. JelleZijlstra
    added a commit that references this issue on May 5, 2025

    gh-82129: Improve annotations for make_dataclass() (#133406)

    bb5ec6e
  18. JelleZijlstra
    closed this as completedin #133406on May 5, 2025
  19. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-82129: Improve annotations for make_dataclass() (python#133406)

    2e9b8e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @ericvsmith
    ericvsmith

Labels

3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifetopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.