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

Better error message for incompatible type assignment when using Generics #8934

Open
rahul-kamat opened this issue Jun 2, 2020 · 9 comments · May be fixed by #12556
Open

Better error message for incompatible type assignment when using Generics #8934

rahul-kamat opened this issue Jun 2, 2020 · 9 comments · May be fixed by #12556

Comments

@rahul-kamat
Copy link

  • Are you reporting a bug, or opening a feature request?

Feature Request

  • Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar


T = TypeVar("T", int, float)


class Foo(Generic[T]):
    def __init__(self, val: T):
        self.data: T = val 


def bar(x: T) -> Foo[T]:
    return Foo(x)


x = bar(1)
x = bar(2.0) # error thrown by mypy
  • What is the actual behavior/output?
    mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
  • What is the behavior/output you expect?

This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.

Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]

  • What are the versions of mypy and Python you are using?

mypy 0.770 and Python 3.7.6

  • What are the mypy flags you are using? (For example --strict-optional)

None

@ethanhs
Copy link
Collaborator

ethanhs commented Jun 3, 2020

I do think the diagnostic could be better. Perhaps something like:

error: Argument 1 to "bar" has incompatible type "float"; expected "int" due to target type "Foo[int]"

@sanlf
Copy link

sanlf commented Jun 4, 2020

Hi, I'm new to open source contribution. Could I try to solve this issue with the message @ethanhs mentioned?

@ethanhs
Copy link
Collaborator

ethanhs commented Jun 4, 2020

@sanlf Welcome! I think the specifics of the output of the diagnostic need more discussion. I would recommend maybe trying to tackle another issue.

@sanlf
Copy link

sanlf commented Jun 4, 2020

@ethanhs Understood. I will try to find another good-first-issue.

@mental32
Copy link

mental32 commented Jun 8, 2020

@eternal861 How about something more conversational?

error: expected "{expected_type}" due to the target type "{name}: {target_type}"; {failure_reason}

Would expand to

error: expected "int" due to the target type "x: Foo[int]"; Argument 1 to "bar" produces incompatible type "Foo[float]"

Disclaimer: I'm new to the codebase/conventions used in mypy but looking to contribute where I can

@himanshu007-creator
Copy link

@eternal861 Sir, i would like to suggest
error: In line (eg 52) bar(Argument), Argument expected as "int" got "float"

@himanshu007-creator
Copy link

  • Are you reporting a bug, or opening a feature request?

Feature Request

  • Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar


T = TypeVar("T", int, float)


class Foo(Generic[T]):
    def __init__(self, val: T):
        self.data: T = val 


def bar(x: T) -> Foo[T]:
    return Foo(x)


x = bar(1)
x = bar(2.0) # error thrown by mypy
  • What is the actual behavior/output?
    mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
  • What is the behavior/output you expect?

This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.

Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]

  • What are the versions of mypy and Python you are using?

mypy 0.770 and Python 3.7.6

  • What are the mypy flags you are using? (For example --strict-optional)

None

can you tell the exact file location to be modified?

@srbhss
Copy link

srbhss commented Mar 29, 2021

Interested in working on this issue. @ethanhs can you please assign me to this issue.

@SwagatSBhuyan
Copy link

Hi, I'd like to tackle this issue. I am still new to the whereabouts of different scripts in the repository, but I guess the error message can be modified in the messages.py script. It would be really great if someone guided me as to where the rest of the important function definitions are, such as isinstance, outer_context, etc. @ethanhs

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

Successfully merging a pull request may close this issue.

8 participants