`bool` is available even if `*constraints` of `TypeVar` is the exact types `int` and `float`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
*Memo:
mypy --strict test.py- mypy 1.18.2
- Python 3.14.0
- Windows 11
The doc says 'Must be exactly str or bytes' about *constraints to make difference between bound and *constraints as shown below:
*Memo:
- From my understanding:
- Setting
bound, the type and the subtypes are available. - Setting
*constraints, only the exact types are available but not the subtypes.
- Setting
T = TypeVar('T') # Can be anything
S = TypeVar('S', bound=str) # Can be any subtype of str
A = TypeVar('A', str, bytes) # Must be exactly str or bytes
But bool is available even if *constraints is the exact types int and float as shown below:
<With TypeVar (Old syntax)>:
from typing import TypeVar, Generic
T = TypeVar('T', int, float)
class MyCls(Generic[T]):
num: T
def __init__(self, x: T) -> None:
self.num = x
# ↓↓↓↓↓↓↓↓↓↓↓↓
mycls = MyCls[bool](True)
# No error
<Without TypeVar (New syntax)>:
class MyCls[T: (int, float)]:
num: T
def __init__(self, x: T) -> None:
self.num = x
# ↓↓↓↓↓↓↓↓↓↓↓↓
mycls = MyCls[bool](True)
# No error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied mypy --strict test.py reproduction with mypy 1.18.2 and compare the old and new TypeVar syntaxes. Trace the type-checking behavior for constrained int/float TypeVars; done means the implementation consistently applies the documented constraint semantics and regression coverage captures the result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100