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 — typing: get_type_hints on stringified lone ClassVar raises TypeError · Issue #90711 · python/cpython

typing: get_type_hints on stringified lone ClassVar raises TypeError #90711

New issue
Copy link
New issue
Copy link
Closed
Closed
typing: get_type_hints on stringified lone ClassVar raises TypeError#90711
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard 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

GBeauregard
mannequin
opened on Jan 27, 2022
Issue body actions
BPO 46553
Nosy @gvanrossum, @ericvsmith, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @AlexWaygood, @GBeauregard
PRs
  • bpo-46553: allow bare typing.ClassVar annotations #30983
  • 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 = <Date 2022-03-17.14:34:07.793>
    created_at = <Date 2022-01-27.20:50:13.975>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'typing: get_type_hints on stringified lone ClassVar raises TypeError'
    updated_at = <Date 2022-03-17.14:34:07.793>
    user = 'https://github.com/GBeauregard'

    bugs.python.org fields:

    activity = <Date 2022-03-17.14:34:07.793>
    actor = 'JelleZijlstra'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-03-17.14:34:07.793>
    closer = 'JelleZijlstra'
    components = ['Library (Lib)']
    creation = <Date 2022-01-27.20:50:13.975>
    creator = 'GBeauregard'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46553
    keywords = ['patch']
    message_count = 11.0
    messages = ['411923', '411924', '411927', '411928', '411934', '411943', '411955', '411959', '411972', '412015', '415413']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'eric.smith', 'JelleZijlstra', 'sobolevn', 'kj', 'AlexWaygood', 'GBeauregard']
    pr_nums = ['30983']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46553'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. GBeauregard commented on Jan 27, 2022

      @GBeauregard
      GBeauregardmannequin
      on Jan 27, 2022
      MannequinAuthor
      More actions
      class C:
          a: "ClassVar"
      
      get_type_hints(C, globals())  # TypeError: Plain typing.ClassVar is not valid as type argument
      

      A stringified lone ClassVar raises at runtime, but this pattern is tested for in dataclasses unit tests and used in the wild. The PEP is not clear that it should or should not be used with arguments, and it works fine when not stringified.

      The fix for this is trivial and I can submit a patch if there's agreement.

    2. GBeauregard
      added
      3.9 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      3.11only security fixesonly security fixes
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      on Jan 27, 2022
    3. GBeauregard commented on Jan 27, 2022

      @GBeauregard
      GBeauregardmannequin
      on Jan 27, 2022
      MannequinAuthor
      More actions

      I think this is needed for moving dataclasses to using typing.py introspection tools to be viable.

    4. GBeauregard
      added
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Jan 27, 2022
    5. ericvsmith commented on Jan 27, 2022

      @ericvsmith
      ericvsmith
      on Jan 27, 2022
      Member
      More actions

      dataclasses is no doubt too lenient. But it's just trying to accept valid strings that look like ClassVar. Way back when this was initially implemented, we decided that calling get_type_hints would be too expensive for every dataclass, and would also neccesitate importing typing, which we didn't want to require.

      I think someone needs to do an analysis of how expensive it would be for dataclasses to import typing and to call get_type_hints. Perhaps we'd make a different decision today.

    6. GBeauregard commented on Jan 27, 2022

      @GBeauregard
      GBeauregardmannequin
      on Jan 27, 2022
      MannequinAuthor
      More actions

      I'm drafting an implementation for the purpose of investigating performance right now; I will share when ready.

    7. gvanrossum commented on Jan 27, 2022

      @gvanrossum
      gvanrossum
      on Jan 27, 2022
      Member
      More actions

      It seems acceptable to mypy.

      I'm not sure I like the agenda of "moving dataclasses to using typing.py introspection tools".

    8. GBeauregard commented on Jan 28, 2022

      @GBeauregard
      GBeauregardmannequin
      on Jan 28, 2022
      MannequinAuthor
      More actions

      It's acceptable to mypy, and pyright added support a few months ago when I made an issue and Eric Traut discovered this pattern in the wild too.

      Some of the other type checkers (pyre) still error I believe. My feeling is that since this has apparently become used in practice we should fix the runtime error for when it's stringified, but we don't necessarily need to prescribe that it's a legal type annotation for type checkers (?). If I was prescribing how to use ClassVar in a vacuum, I don't see any good typing reason we should prohibit this.

      re: moving dataclasses: I'm not sure either! I'm trying to look into the issues it brings up in practice to weigh against the difficulties of maintaining the existing bespoke introspection implementation and the problems it has (dealing with stringified annotations, supporting renaming the Annotated symbol).

      I made an implementation that fully moves dataclasses over to using get_type_hints (and got all the tests to pass), but my feeling right now is that a few issues are too serious for it to be viable:

      1. the namespaces are a mess, and issues like get_type_hints() always raises on forward references typing#508 don't have a solution at the moment
      2. get_type_hints paints a wide brush on the entire class when it raises for errors, so it's not very viable to deal with non-typing non-Annotated[] annotations as a one-off

      There's also nontechnical issues, like the potential politics of making dataclasses always import typing. I'll be updating bpo-46511 later with thoughts.

    9. gvanrossum commented on Jan 28, 2022

      @gvanrossum
      gvanrossum
      on Jan 28, 2022
      Member
      More actions

      Please just don't go there. I beg you. It's not worth it.

    10. GBeauregard commented on Jan 28, 2022

      @GBeauregard
      GBeauregardmannequin
      on Jan 28, 2022
      MannequinAuthor
      More actions

      I think I miscommunicated my intent with sentence placement. I already posted the thoughts I referred to; they're just my concluding opinion on the technical merit of using get_type_hints in dataclasses to solve the Annotated problem: https://bugs.python.org/msg411945

      In any case, I apologize for the faux pas and I'll be careful in the future.

      I dug up the issue where pyright was changed (to allow bare ClassVar) that has an argument for not allowing it to be bare: microsoft/pyright#2377

    11. gvanrossum commented on Jan 28, 2022

      @gvanrossum
      gvanrossum
      on Jan 28, 2022
      Member
      More actions

      I
      To be clear: I am okay with this patch, just not with making dataclasses
      import typing.

      --Guido (mobile)

    12. gvanrossum commented on Jan 28, 2022

      @gvanrossum
      gvanrossum
      on Jan 28, 2022
      Member
      More actions

      New changeset 5445e17 by Gregory Beauregard in branch 'main':
      bpo-46553: allow bare typing.ClassVar annotations (bpo-30983)
      5445e17

    13. ericvsmith commented on Mar 17, 2022

      @ericvsmith
      ericvsmith
      on Mar 17, 2022
      Member
      More actions

      Is there any reason to keep this issue open? The PR was merged.

    14. JelleZijlstra
      closed this as completedon Mar 17, 2022
    15. JelleZijlstra
      closed this as completedon Mar 17, 2022
    16. ezio-melotti
      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 lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard 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 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.