Repository navigation
[Reverted] Make all single-constraint TypeVars to use bounds - #804
Conversation
According to the documentation in the typing module, TypeVars cannot have only a single constraint. Attempting to do so will actually result in an exception at runtime. (However, this error is currently ignored by mypy -- see python/mypy#2626 for a related pending pull request). This commit changes all instances of TypeVars using a single constraint (e.g. `T = TypeVar('T', Foo)`) to use bounds instead (e.g. `T = TypeVar('T', bound=Foo)`. This seems to be the correct fix for plistlib after reading the module docs, but it's less obvious this is correct for unittest. The unittest module originally had `_FT = TypeVar('_FT', Callable[[Any], Any])` -- an alternative fix would have been to do `_FT = Callable[[Any], Any]`. Although I'm not entirely sure what it means to have a bound be a Callable, I decided to make the assumption that the original authors probably meant to use TypeVars instead of type aliases for a reason (possibly to handle classes implementing `__call__`?)
|
Also unsure about the unittest case but we can fix it forward if some edge reveals we should have gone with |
|
Now that I've read this over carefully, I unfortunately think that the unittest fix is incorrect. I have this simple test: from unittest import skip, TestCase
class TC(TestCase):
@skip("not today")
def testFoo(self) -> None:
assert FalseBefore this PR, that works, but with the change to I believe the problem is that you currently can't return a generic type from a function without generic arguments. Apparently the old form worked around this (perhaps it just removed the genericity from the return type, which makes sense as an edge case of a TypeVar with value constraints). There's an open issue for this: python/mypy#1551 So sadly I'm going to revert this -- we first need to fix the latter issue. :-( |
Reverts #804. Reason: until python/mypy#1551 is fixed this gives an error whenever @Skip() is used. Specifically see #804 (comment).
|
Bleh, I figured there was something odd afoot. I'm not at a computer right now, but I can poke around/try and find a workaround sometime later today or tomorrow. |
|
@gvanrossum -- I'm slowly poking around with the TypeVar stuff, but in the interim, do you think using |
|
Yeah, I think |
|
I'm going to attempt a new band-aid fix so we can finally move forward with #2626. |
…1118) Another attempt for #804, to unblock python/mypy#2626. There's a work-around here for python/mypy#1551.
According to the documentation in the typing module, TypeVars cannot have only a single constraint. Attempting to do so will actually result in an exception at runtime. (However, this error is currently ignored by mypy -- see python/mypy#2626 for a related pending pull request).
This commit changes all instances of TypeVars using a single constraint (e.g.
T = TypeVar('T', Foo)) to use bounds instead (e.g.T = TypeVar('T', bound=Foo).This seems to be the correct fix for plistlib after reading the module docs, but it's less obvious this is correct for unittest. The unittest module originally had
_FT = TypeVar('_FT', Callable[[Any], Any])-- an alternative fix would have been to do_FT = Callable[[Any], Any].Although I'm not entirely sure what it means to have a bound be a Callable, I decided to make the assumption that the original authors probably meant to use TypeVars instead of type aliases for a reason (possibly to handle classes implementing
__call__?)