Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing: allow Annotated in outermost scope #90649

Closed
GBeauregard mannequin opened this issue Jan 23, 2022 · 13 comments
Closed

typing: allow Annotated in outermost scope #90649

GBeauregard mannequin opened this issue Jan 23, 2022 · 13 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@GBeauregard
Copy link
Mannequin

GBeauregard mannequin commented Jan 23, 2022

BPO 46491
Nosy @gvanrossum, @JelleZijlstra, @asottile, @miss-islington, @Fidget-Spinner, @GBeauregard
PRs
  • bpo-46491: Allow Annotated on outside of Final/ClassVar #30864
  • [3.10] bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864) #30873
  • [3.9] bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864) #30874
  • 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-01-25.14:40:07.659>
    created_at = <Date 2022-01-23.19:03:20.373>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'typing: allow Annotated in outermost scope'
    updated_at = <Date 2022-01-26.00:26:54.760>
    user = 'https://github.com/GBeauregard'

    bugs.python.org fields:

    activity = <Date 2022-01-26.00:26:54.760>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-25.14:40:07.659>
    closer = 'kj'
    components = ['Library (Lib)']
    creation = <Date 2022-01-23.19:03:20.373>
    creator = 'GBeauregard'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46491
    keywords = ['patch']
    message_count = 13.0
    messages = ['411404', '411405', '411418', '411557', '411598', '411599', '411600', '411686', '411688', '411690', '411691', '411692', '411693']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'JelleZijlstra', 'Anthony Sottile', 'miss-islington', 'kj', 'GBeauregard']
    pr_nums = ['30864', '30873', '30874']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46491'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Jan 23, 2022

    Currently, typing.Annotated (PEP-593) cannot be used at runtime with typing.Final and typing.ClassVar with Annotated on the outside:

    from typing import Annotated, Final
    # TypeError: typing.Final[int] is not valid as type argument
    var: Annotated[Final[int], "foo"] = 4
    

    The only tenuously related mention of this I can find in a PEP is in PEP-593 (Annotated) which states "The first argument to Annotated must be a valid type".

    I believe the runtime behavior should be changed to allow any ordering for Annotated with ClassVar and Final. This was discussed in the typing-sig PEP-655 thread (TypedDict Required and NotRequired) where the current plan is to allow Required/NotRequired in any nesting order with Annotated while suggesting the ClassVar/Final ordering restriction be lifted: https://mail.python.org/archives/list/typing-sig@python.org/message/22CJ5TJGIJ563D6ZKB7R3VUZXTZQND5X/

    The argument for doing so is on the mailing list: https://mail.python.org/archives/list/typing-sig@python.org/message/MPMOIBX3XFXCD4ZNDC6AV4CLSI5LN544/

    To summarize: adopting an overly strict view of what constitutes a valid type for Annotated creates difficulties for people who use runtime introspection of Annotated annotations by requiring them to parse additional typing or field annotations (https://bugs.python.org/msg411067). This needlessly exacerbates tension between typing and non-typing uses of annotation space. In order to be a good citizen to other annotation users, the Annotated runtime ordering restriction should be lifted.

    @GBeauregard GBeauregard mannequin added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes type-bug An unexpected behavior, bug, or error labels Jan 23, 2022
    @JelleZijlstra
    Copy link
    Member

    I support making this change. It looks like the simplest implementation is simply to remove the mention of ClassVar and Final in typing._type_check.

    @JelleZijlstra JelleZijlstra added stdlib Python modules in the Lib dir labels Jan 23, 2022
    @gvanrossum
    Copy link
    Member

    +1--
    --Guido (mobile)

    @miss-islington
    Copy link
    Contributor

    New changeset e1abffc by Gregory Beauregard in branch 'main':
    bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864)
    e1abffc

    @miss-islington
    Copy link
    Contributor

    New changeset 41e0aea by Miss Islington (bot) in branch '3.10':
    bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864)
    41e0aea

    @miss-islington
    Copy link
    Contributor

    New changeset b0b8388 by Miss Islington (bot) in branch '3.9':
    bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864)
    b0b8388

    @Fidget-Spinner
    Copy link
    Member

    Thanks Gregory for fixing this!

    @asottile
    Copy link
    Mannequin

    asottile mannequin commented Jan 25, 2022

    should this behaviour change be backported? it potentially creates an annoying edgecase where code seemingly works unless you use an older patch version

    since this isn't a bugfix I wouldn't expect this to land in 3.9 and 3.10

    @gvanrossum
    Copy link
    Member

    But it *is* a bugfix.

    @asottile
    Copy link
    Mannequin

    asottile mannequin commented Jan 25, 2022

    to me this is the same as the Union[Pattern] / Union[Match] "fixes" that landed in 3.5.3 -- and the pain caused by using that and having CI pass (because of modern 3.5.x) but having spurious bug reports from users stuck on 3.5.2

    or in 3.6.1 when NamedTuple was "fixed" to allow methods, again having CI pass with modern pythons but having frustrated users running 3.6.0

    I forsee the same class of problems here with Annotated where it works great in 3.10.3 and 3.9.11 but anyone stuck on 3.10.2 or 3.9.10 or older will be broken

    @gvanrossum
    Copy link
    Member

    Well that's *always* a problem right? If you take that to the extreme we wouldn't need bugfix releases. :-)

    Apart from some examples in the 3.5-3.6 timeframe, what makes you think that *this* fix *specifically* is going to make more people unhappy than it makes happy?

    When people complain that they wrote Annotated[ClassVar[int], ...] and are disappointed that it doesn't work in a certain old version, you can just tell them to use ClassVar[Annotated[int, ...]] which is in no way invalid or inferior and works in all versions that support Annotated.

    @asottile
    Copy link
    Mannequin

    asottile mannequin commented Jan 26, 2022

    3.7.2 has another example where OrderedDict was added to typing

    I don't have any personal investment in this particular change but I've had quite a few unhappy consumers of libraries due to instability in typing apis between patch versions in the past (heh and they're especially frustrated because they don't care about type checking and just want functional software)

    it's also difficult and/or not cost effective to add every patch version to a CI matrix to catch these sorts of problems

    @gvanrossum
    Copy link
    Member

    Yeah, there are no perfect solutions. Please let it go.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants