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

TopologicalSorter is not Generic at runtime (but is in typeshed) #89522

Closed
JacobHayes mannequin opened this issue Oct 3, 2021 · 7 comments
Closed

TopologicalSorter is not Generic at runtime (but is in typeshed) #89522

JacobHayes mannequin opened this issue Oct 3, 2021 · 7 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@JacobHayes
Copy link
Mannequin

JacobHayes mannequin commented Oct 3, 2021

BPO 45359
Nosy @asvetlov, @AlexWaygood, @JacobHayes
PRs
  • bpo-45359: Support TopologicalSorter type subscript #28714
  • 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 2021-12-09.11:36:14.329>
    created_at = <Date 2021-10-03.22:18:50.416>
    labels = ['type-feature', 'library', '3.11']
    title = 'TopologicalSorter is not Generic at runtime (but is in typeshed)'
    updated_at = <Date 2021-12-10.14:59:57.694>
    user = 'https://github.com/JacobHayes'

    bugs.python.org fields:

    activity = <Date 2021-12-10.14:59:57.694>
    actor = 'JacobHayes'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-09.11:36:14.329>
    closer = 'asvetlov'
    components = ['Library (Lib)']
    creation = <Date 2021-10-03.22:18:50.416>
    creator = 'JacobHayes'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45359
    keywords = ['patch']
    message_count = 7.0
    messages = ['403115', '408041', '408109', '408110', '408117', '408177', '408211']
    nosy_count = 3.0
    nosy_names = ['asvetlov', 'AlexWaygood', 'JacobHayes']
    pr_nums = ['28714']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue45359'
    versions = ['Python 3.11']

    @JacobHayes
    Copy link
    Mannequin Author

    JacobHayes mannequin commented Oct 3, 2021

    Reproduction:

    from graphlib import TopologicalSorter
    
    TopologicalSorter[str]({"a": {}, "b": {"a"}})
    
    $ mypy /tmp/toposort.py
    Success: no issues found in 1 source file
    $ python3 /tmp/toposort.py
    Traceback (most recent call last):
      File "/tmp/toposort.py", line 3, in <module>
        TopologicalSorter[str]({"a": {}, "b": {"a"}})
    TypeError: 'type' object is not subscriptable
    

    I opened the issue here (rather than typeshed) because we'd presumably like to support this at runtime too.

    Typeshed link: https://github.com/python/mypy/blob/0a830481980bfc554ded61a3eaaaecde384a21e4/mypy/typeshed/stdlib/graphlib.pyi#L6

    @JacobHayes JacobHayes mannequin added 3.9 only security fixes type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Oct 3, 2021
    @AlexWaygood AlexWaygood added 3.10 only security fixes 3.11 only security fixes labels Nov 1, 2021
    @asvetlov
    Copy link
    Contributor

    asvetlov commented Dec 8, 2021

    New changeset 3cb9731 by Jacob Hayes in branch 'main':
    bpo-45359: Support TopologicalSorter type subscript (GH-28714)
    3cb9731

    @asvetlov
    Copy link
    Contributor

    asvetlov commented Dec 9, 2021

    The new feature is applied to Python 3.11 only

    @asvetlov asvetlov removed 3.9 only security fixes 3.10 only security fixes labels Dec 9, 2021
    @asvetlov asvetlov closed this as completed Dec 9, 2021
    @asvetlov asvetlov removed 3.9 only security fixes 3.10 only security fixes labels Dec 9, 2021
    @asvetlov asvetlov closed this as completed Dec 9, 2021
    @JacobHayes
    Copy link
    Mannequin Author

    JacobHayes mannequin commented Dec 9, 2021

    Thanks for merging!

    Should typeshed be updated for <3.11 in the meantime or do you suggest if TYPE_CHECKING blocks on user side? Perhaps it's a non-issue if no one else has noticed this. :)

    @asvetlov
    Copy link
    Contributor

    asvetlov commented Dec 9, 2021

    `if TYPE_CHECKING:` is a good choice, it works fine just now.
    `from __future__ import annotations` is another alternative.

    I don't see a reason for REMOVING already existing and correct annotations from typeshed.

    @AlexWaygood
    Copy link
    Member

    Another option for code using Python <3.11, that will work without the from __future__ import annotations import, is to do something like this:

    from graphlib import TopologicalSorter
    x: 'TopologicalSorter[str]' = TopologicalSorter({"a": {}, "b": {"a"}})
    

    By using a string as the annotation, we give mypy the specificity it needs, but the expression will never need to be resolved at runtime.

    @AlexWaygood AlexWaygood added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Dec 10, 2021
    @JacobHayes
    Copy link
    Mannequin Author

    JacobHayes mannequin commented Dec 10, 2021

    Thanks for the tips! I've been using this patch in my own code in a early imported __init__.py:

    from graphlib import TopologicalSorter
    from types import GenericAlias
    
    if not hasattr(TopologicalSorter, "__class_getitem__"):  # pragma: no cover
        TopologicalSorter.__class_getitem__ = classmethod(GenericAlias)  # type: ignore
    

    Certainly a bit hacky, but aside from no-op on >=3.11 and satisfying mypy, it supports runtime type inspection via get_type_hints before 3.11 (which otherwise still errors for stringized versions). The stringized versions are probably a bit simpler/safer for most folks though :)

    @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.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants