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: Classes that inherit Generic[...] indirectly aren't considered generic. #82640

Closed
JohnLennon mannequin opened this issue Oct 13, 2019 · 4 comments
Closed

typing: Classes that inherit Generic[...] indirectly aren't considered generic. #82640

JohnLennon mannequin opened this issue Oct 13, 2019 · 4 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JohnLennon
Copy link
Mannequin

JohnLennon mannequin commented Oct 13, 2019

BPO 38459
Nosy @gvanrossum, @ilevkivskyi

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 2019-10-13.10:43:13.496>
created_at = <Date 2019-10-13.08:31:23.929>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = "typing: Classes that inherit `Generic[...]` indirectly aren't considered generic."
updated_at = <Date 2019-10-13.10:43:13.489>
user = 'https://bugs.python.org/JohnLennon'

bugs.python.org fields:

activity = <Date 2019-10-13.10:43:13.489>
actor = 'levkivskyi'
assignee = 'none'
closed = True
closed_date = <Date 2019-10-13.10:43:13.496>
closer = 'levkivskyi'
components = ['Library (Lib)']
creation = <Date 2019-10-13.08:31:23.929>
creator = 'John Lennon'
dependencies = []
files = []
hgrepos = []
issue_num = 38459
keywords = []
message_count = 4.0
messages = ['354568', '354569', '354570', '354572']
nosy_count = 3.0
nosy_names = ['gvanrossum', 'levkivskyi', 'John Lennon']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue38459'
versions = ['Python 3.7']

@JohnLennon
Copy link
Mannequin Author

JohnLennon mannequin commented Oct 13, 2019

Given the file example.py with the following contents:

from typing import Generic, TypeVar

KT = TypeVar("KT")
VT = TypeVar("VT")


class GenericMapping(Generic[KT, VT]):
    pass


class SomeImplMapping(GenericMapping):
    pass


a: GenericMapping[int, float]
b: SomeImplMapping[int, float]

I would expect SomeImplMapping to be generic as well as GenericMapping. However, currently this code fails with the following error:

Traceback (most recent call last):
  File "adt.py", line 18, in <module>
    b: SomeImplMapping[int, float]
  File "/usr/local/lib/python3.7/typing.py", line 254, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.7/typing.py", line 841, in __class_getitem__
    _check_generic(cls, params)
  File "/usr/local/lib/python3.7/typing.py", line 204, in _check_generic
    raise TypeError(f"{cls} is not a generic class")
TypeError: <class '__main__.SomeImplMapping'> is not a generic class

If I understand everything correctly, that's because typing doesn't check bases of the class to have __parameters__ attribute:

https://github.com/python/cpython/blob/master/Lib/typing.py#L210

I did not found the restriction that only direct childs of Generic[..] class can be generic in the docs, so I think this is a bug.

@JohnLennon JohnLennon mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 13, 2019
@JohnLennon
Copy link
Mannequin Author

JohnLennon mannequin commented Oct 13, 2019

However, if I change the signature to:

class SomeImplMapping(GenericMapping[KT, VT]):

Everything works just fine. And that's not really clear for me: we already have declared the generic types, we can not change the amount of those arguments, so why do we have to pass the same generic parameters again?

And even if that's the designed way to do things, I would expect the first example to fail not because of the type substitution, but for deriving the GenericMapping without generic type arguments provided (as if I'll try to derive Generic instead of Generic[...]).

@JohnLennon
Copy link
Mannequin Author

JohnLennon mannequin commented Oct 13, 2019

BTW I don't mind creating a PR for that issue if there will be an agreement on what is the desired behavior here.

@ilevkivskyi
Copy link
Member

The docs for typing module are clear about this:

Using a generic class without specifying type parameters assumes Any for each position.

There is also an example involving a base class.

@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.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant