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

GenericAlias does not support nested type variables #84588

Closed
serhiy-storchaka opened this issue Apr 27, 2020 · 6 comments
Closed

GenericAlias does not support nested type variables #84588

serhiy-storchaka opened this issue Apr 27, 2020 · 6 comments
Assignees
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 40408
Nosy @gvanrossum, @serhiy-storchaka, @ilevkivskyi
PRs
  • bpo-40408: Fix support of nested type variables in GenericAlias. #19836
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2020-05-04.08:00:19.205>
    created_at = <Date 2020-04-27.15:57:57.150>
    labels = ['interpreter-core', 'type-bug', '3.9']
    title = 'GenericAlias does not support nested type variables'
    updated_at = <Date 2020-05-04.08:30:23.949>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2020-05-04.08:30:23.949>
    actor = 'levkivskyi'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2020-05-04.08:00:19.205>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2020-04-27.15:57:57.150>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40408
    keywords = ['patch']
    message_count = 6.0
    messages = ['367433', '367454', '367506', '367944', '368014', '368019']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'levkivskyi']
    pr_nums = ['19836']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40408'
    versions = ['Python 3.9']

    @serhiy-storchaka
    Copy link
    Member Author

    While trying to replace typing._GenericAlias with GenericAlias I have found that the latter does not support nested type variables.

    >>> from typing import *
    >>> T = TypeVar('T')
    >>> X = List[List[T]]
    >>> X.__parameters__
    (~T,)
    >>> X[int]
    typing.List[typing.List[int]]
    >>> Y = list[list[T]]
    >>> Y.__parameters__
    ()
    >>> Y[int]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: There are no type variables left in list[list[~T]]

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 27, 2020
    @gvanrossum
    Copy link
    Member

    Good catch. Is there a reasonable fix?

    @serhiy-storchaka
    Copy link
    Member Author

    Not yet. If you don't prefer to fix it yourself, I'll do it as soon as I have time.

    @serhiy-storchaka
    Copy link
    Member Author

    There is a difference between PR 19836 and the typing module in handling nested unsubscribed generic aliases:

    >>> from typing import *
    >>> T = TypeVar('T')
    >>> D1 = Dict[T, List]
    >>> D2 = dict[T, List]
    >>> D1.__parameters__
    (~T,)
    >>> D1[int]
    typing.Dict[int, typing.List[~T]]
    >>> D1[int].__parameters__
    (~T,)
    >>> D1[int][str]
    typing.Dict[int, typing.List[str]]
    >>> D1[int, str]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/serhiy/py/cpython/Lib/typing.py", line 267, in inner
        return func(*args, **kwds)
      File "/home/serhiy/py/cpython/Lib/typing.py", line 686, in __getitem__
        _check_generic(self, params)
      File "/home/serhiy/py/cpython/Lib/typing.py", line 221, in _check_generic
        raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
    TypeError: Too many parameters for typing.Dict[~T, typing.List]; actual 2, expected 1
    >>> D2.__parameters__
    (~T, ~T)
    >>> D2[int]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Too few arguments for dict[~T, typing.List]
    >>> D2[int, str]
    dict[int, typing.List[str]]

    But this behavior is not specified and is not covered by tests.

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 41a6458 by Serhiy Storchaka in branch 'master':
    bpo-40408: Fix support of nested type variables in GenericAlias. (GH-19836)
    41a6458

    @ilevkivskyi
    Copy link
    Member

    But this behavior is not specified and is not covered by tests.

    FWIW, to be most close to the static type checkers behavior, both D[int][str] and D[int, str] should fail for D = Dict[T, List]. Not important however, since this is a really rare corner case I think.

    @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 interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants