Bound TypeVar on generic methods inside generic class
up vote
1
down vote
favorite
For some reason, this code lints up as a problem:
from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")
class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()
Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.
Is this a bug? What am I doing wrong?
I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.
python-3.x generics typing type-variables
add a comment |
up vote
1
down vote
favorite
For some reason, this code lints up as a problem:
from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")
class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()
Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.
Is this a bug? What am I doing wrong?
I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.
python-3.x generics typing type-variables
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
For some reason, this code lints up as a problem:
from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")
class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()
Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.
Is this a bug? What am I doing wrong?
I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.
python-3.x generics typing type-variables
For some reason, this code lints up as a problem:
from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")
class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()
Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.
Is this a bug? What am I doing wrong?
I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.
python-3.x generics typing type-variables
python-3.x generics typing type-variables
asked Jun 26 at 12:01
Bharel
6,14411638
6,14411638
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the@classmethodto confirm my hunch.)
– Michael0x2a
Jun 30 at 1:16
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the@classmethodto confirm my hunch.)
– Michael0x2a
Jun 30 at 1:16
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
add a comment |
up vote
2
down vote
accepted
I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the@classmethodto confirm my hunch.)
– Michael0x2a
Jun 30 at 1:16
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.
I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.
answered Jun 28 at 13:12
Michael0x2a
21.4k1671124
21.4k1671124
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the@classmethodto confirm my hunch.)
– Michael0x2a
Jun 30 at 1:16
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
add a comment |
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the@classmethodto confirm my hunch.)
– Michael0x2a
Jun 30 at 1:16
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
– Bharel
Jun 29 at 13:26
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
@Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
– Michael0x2a
Jun 30 at 1:14
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the
@classmethod to confirm my hunch.)– Michael0x2a
Jun 30 at 1:16
In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the
@classmethod to confirm my hunch.)– Michael0x2a
Jun 30 at 1:16
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
– Michael0x2a
Jun 30 at 1:18
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
Thank you very much for the extended explanation! You're awesome!
– Bharel
Jun 30 at 8:43
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51042452%2fbound-typevar-on-generic-methods-inside-generic-class%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password