Skip to content
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

Generics is not supported with some operators #6157

Open
appraveen opened this issue Jan 7, 2019 · 1 comment
Open

Generics is not supported with some operators #6157

appraveen opened this issue Jan 7, 2019 · 1 comment

Comments

@appraveen
Copy link

  • 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

@ktbarrett
Copy link

@appraveen
In your example it looks like the TypeVar T is not used. You can simply replace it with Union[int, float].

S = TypeVar('S', bound=Union[int, float])

def some_function(a: S, b: Union[int, float]) -> S:
    # do some operation and returns the type based on first parameter.
    c = a + b
    ... # some_computed_variable would be finally computed and has the value based on first parameter type.
    return some_computed_variable

This seems to be a problem specifically with unions. Changing the bound on T to a concrete type works just fine. mypy is not trying to test every branch of the union for feasibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants