Skip to content

Type annotations in _colorize module are unresolvable at runtime #150994

Description

@gotmax23

Bug report

Bug description:

from annotationlib import get_annotations
from _colorize import ThemeSection
get_annotations(ThemeSection) # NameError: name 'ClassVar' is not defined

See the above reproducer and beartype/beartype#656 for more context. The issue here is that the _colorize module (which is imported by other public modules/packages in the stdlib) guards its typing imports (which are used in dataclass field annotations) in an if False: block. This breaks runtime type checkers like beartype that introspect type annotations at runtime.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.15bugs and security fixes
    on Jun 6, 2026
  2. hugovk commented on Jun 6, 2026

    @hugovk
    Member

    The trivial fix in "Easy Fixes: If Only Buggy People Were This Easy to Fix" (beartype/beartype#656 (comment)):

    # This obviously useless "if" conditional...
    if False:
        from typing import IO, Literal, Self, ClassVar
        _theme: Theme  # <-- no idea what this is all about. smells fishy
    
    # ...should instead be a proper PEP 810 lazy import:
    lazy from typing import IO, Literal, Self, ClassVar
    
    # ...while preserving this smelly fishy code, because someone will yell if we don't:
    if False:
        _theme: Theme  # <-- preserved. but still smells fishy

    means the typing module still ends up eagerly imported, due to the Literal.

    This prevents an eager typing import and fixes the reproducer:

    lazy from typing import IO, Self, ClassVar
    
    # types
    if False:
        from typing import Literal
        _theme: Theme
  3. added
    3.16new features, bugs and security fixes
    on Jun 6, 2026
  4. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    Contributor

    This leaves us with a new reproducer of:

    from annotationlib import get_annotations
    from _colorize import LiveProfiler
    get_annotations(LiveProfiler)

    I'm not sure if this will also trip up beartype. One mildly sneaky thought is to put a lazy import at the end of the module after the dataclasses have done their work.

    Also this technically fails in Python 3.14 too, earlier Python used __future__ annotations so technically won't suffer from the get_annotations issue.

  5. added a commit that references this issue on Jun 6, 2026
  6. JelleZijlstra commented on Jun 6, 2026

    @JelleZijlstra
  7. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
  8. JelleZijlstra commented on Jun 6, 2026

    @JelleZijlstra
    Member

    This PR might help: #149992 (though I haven't tested the issue's specific repro).

  9. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    Contributor

    That would probably help for analysing _pyrepl. I didn't look too closely but I think that will probably always end up importing typing without further changes anyway?

    I don't think we can do the same thing for _colorize as it doesn't import typing but has a Literal in an annotation that would cause the import to be triggered by dataclasses.

    If we do want to resolve this, other than putting the import at the end, we could potentially 'hide' the Literal in a type alias that should prevent typing from being imported when @dataclass gets the annotations. Though that would break if dataclasses ever started looking inside type aliases.

  10. JelleZijlstra commented on Jun 6, 2026

    @JelleZijlstra
    Member

    Ah, right, _colorize not _pyrepl. I opened #151018 because the fix seemed easy and makes the code mostly nicer anyway.

    beartype though should use the FORWARDREF format if it expects to call get_type_hints on arbitrary objects.

  11. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    Contributor

    I think beartype might be using FORWARDREF but at some point later it is attempting to resolve the refs? I see calls to .evaluate() raising in the long log that was posted on the beartype issue.

  12. added 2 commits that reference this issue on Jun 24, 2026
  13. added a commit that references this issue on Sep 10, 2026
  14. added 2 commits that reference this issue on Sep 10, 2026
  15. added a commit that references this issue on Sep 12, 2026
  16. added a commit that references this issue on Sep 14, 2026
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.15bugs and security fixes3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions