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 — Type annotations in `_colorize` module are unresolvable at runtime · Issue #150994 · python/cpython

Type annotations in _colorize module are unresolvable at runtime #150994

New issue
Copy link
New issue
Copy link
Closed
Closed
Type annotations in _colorize module are unresolvable at runtime#150994
Copy link
Labels
3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@gotmax23
gotmax23
opened on Jun 5, 2026
Issue body actions

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

  • gh-150994: _colorize: modernize typing imports #151018
  • [3.15] gh-150994: _colorize: modernize typing imports (GH-151018) #152041
  • gh-150994: remove duplicate NEWS entry #157248
  • [3.15] gh-150994: remove duplicate NEWS entry #157249
  • [3.15] gh-150994: remove duplicate NEWS entry (GH-157248) #157256

Activity

  1. gotmax23
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 5, 2026
  2. DavidCEllis
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.15bugs and security fixesbugs and security fixes
    on Jun 6, 2026
  3. hugovk commented on Jun 6, 2026

    @hugovk
    hugovk
    on Jun 6, 2026
    Member
    More actions

    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
  4. DavidCEllis
    added
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    on Jun 6, 2026
  5. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    DavidCEllis
    on Jun 6, 2026
    Contributor
    More actions

    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.

  6. leycec
    added a commit that references this issue on Jun 6, 2026

    `_colorize` blacklist.

    4a9fa1d
  7. JelleZijlstra commented on Jun 6, 2026

    @JelleZijlstra
    JelleZijlstra
    on Jun 6, 2026 · Hidden as outdated
    show commentMore actions
  8. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    DavidCEllis
    on Jun 6, 2026 · Hidden as outdated
    show commentMore actions
  9. JelleZijlstra commented on Jun 6, 2026

    @JelleZijlstra
    JelleZijlstra
    on Jun 6, 2026
    Member
    More actions

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

  10. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    DavidCEllis
    on Jun 6, 2026
    Contributor
    More actions

    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.

  11. JelleZijlstra commented on Jun 6, 2026

    @JelleZijlstra
    JelleZijlstra
    on Jun 6, 2026
    Member
    More actions

    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.

  12. DavidCEllis commented on Jun 6, 2026

    @DavidCEllis
    DavidCEllis
    on Jun 6, 2026
    Contributor
    More actions

    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.

  13. JelleZijlstra
    added 2 commits that reference this issue on Jun 24, 2026

    gh-150994: _colorize: modernize typing imports (#151018)

    fcda96f

    [3.15] gh-150994: _colorize: modernize typing imports (GH-151018) (#1…

    4677e25
  14. hugovk
    closed this as completedon Jun 24, 2026
  15. JelleZijlstra
    added a commit that references this issue on Sep 10, 2026

    gh-150994: remove duplicate NEWS entry (#157248)

    f715d25
  16. hugovk
    added 2 commits that reference this issue on Sep 10, 2026

    [3.15] pythongh-150994: remove duplicate NEWS entry (pythonGH-157248)

    8c73a47

    [3.15] gh-150994: remove duplicate NEWS entry (#157249)

    98032d7
  17. clin1234
    added a commit that references this issue on Sep 12, 2026

    pythongh-150994: remove duplicate NEWS entry (python#157248)

    64cc762
  18. johnslavik
    added a commit that references this issue on Sep 14, 2026

    pythongh-150994: remove duplicate NEWS entry (python#157248)

    8f944bd
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 fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-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 01402503 — 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.