-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
a Type is not recognized properly for subclassing #10962
Comments
Even this does not work: class Base:
...
def get_base() -> Base:
...
BaseAlias = get_base()
class Derived(BaseAlias):
... Outputs:
I'll see what we can do here. |
I guess that the easiest way to explain this is with this example: from typing import Type
class Base:
...
class NotSoBase(Base):
a = 1
def get_base() -> Type[Base]:
return NotSoBase
BaseAlias: Type[Base] = get_base()
class Derived(BaseAlias):
... This example will raise the same error. But, it differs in what type we return from In your example you expect Mypy to understand that we use |
@sobolevn Would you mind explaining you couterexample a bit more? As far as I understand, using I'm asking because I think I have another example which is related to this issue: |
@yrd I think that this: def get_base() -> Type[Base]:
return NotSoBase
BaseAlias: Type[Base] = get_base()
class Some(BaseAlias): ... Will generate inconsistent MRO for mypy. Imagine that this code is valid. Then, mypy would think that |
Hm, I see that now - thanks! |
This removes the `client.Module` syntax we had before, because that was a nightmare to type correctly. This is more or less due to this[1] issue in mypy. Once we find a good way to make type checkers happy, the syntax may return. But for now, using SyncModule and AsyncModule as a superclass will suffice. 1: python/mypy#10962
I've been hitting what I think is this same issue in bidict too. I've been silently 'type: ignoring' it, but figured I'd chime in here, in case this is another useful test case: def namedbidict(
typename: str,
keyname: str,
valname: str,
*,
base_type: type[BidictBase[KT, VT]] = bidict,
) -> type[BidictBase[KT, VT]]:
...
class NamedBidict(base_type): # false positive errors here (see below)
... $ mypy bidict
bidict/_named.py: note: In function "namedbidict":
bidict/_named.py:72:23: error: Variable "base_type" is not valid as a type
[valid-type]
class NamedBidict(base_type):
^
bidict/_named.py:72:23: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
bidict/_named.py:72:23: error: Invalid base class "base_type" [misc]
class NamedBidict(base_type):
^
Found 2 errors in 1 file (checked 26 source files) |
You can model |
If I understand you correctly, the namedbidict API has been in bidict since 2009, long before Python had type hints. Sharing this example in case it’s desirable for mypy to support such APIs. If not, fair enough! |
This affects setuptool's def get_unpatched(item: _T) -> _T: ...
# Used like:
_Distribution = get_unpatched(distutils.core.Distribution) It also lead to unexpected |
Bug Report
I'm explicitly typing a variable with
Type
(ortype
) and mypy thinks I should not subclass it.To Reproduce
Expected Behavior
There should be no error. I'm subclassing a type, which is ok. Furthermore, mypy has the information that it's a type so subclassing should not be questioned.
Actual Behavior
Same behavior if specifying
BaseAlias: type = get_base()
instead.Your Environment
mypy.ini
(and other config files): nonepython:3.9
docker imageThe text was updated successfully, but these errors were encountered: