Generics is not supported with some operators
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
- Are you reporting a bug, or opening a feature request?
Bug - Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Supporting operators with union does not work with generics.
S = TypeVar('S', bound=Union[int, float])
T = TypeVar('T', bound=Union[int, float])
def some_function(a:S, b:T) -> S:
// do some operation and returns the type based on first parameter.
c = a + b
// if there is an addition involved in the computation, then we would get the below error.
// some_computed_variable would be finally computed and has the value based on first parameter type.
return some_computed_variable
-
What is the actual behavior/output?
error: Unsupported operand types for + ("S" and "T")We can use alias (Union[int, float] as type) instead of generics (
S) as a workaround but it loses generic support. -
What is the behavior/output you expect?
Should not report error -
What are the versions of mypy and Python you are using?
Python: 3.6 mypy: 0.650 -
What are the mypy flags you are using? (For example --strict-optional)
--ignore-missing-imports
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 reproducing the generic operator case from the issue with a current mypy checkout, then review the supporting-operators-with-union change in PR #5545. Trace how bounded type variables are checked for addition and add coverage showing compatible generic operands are accepted without losing the return type based on the first parameter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100