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

Confusing error message when using AnyStr #963

Open
oconnor663 opened this issue Oct 22, 2015 · 6 comments
Open

Confusing error message when using AnyStr #963

oconnor663 opened this issue Oct 22, 2015 · 6 comments
Labels
bug mypy got something wrong priority-0-high topic-join-v-union Using join vs. using unions topic-usability

Comments

@oconnor663
Copy link

from typing import AnyStr

def f(x: AnyStr, y: AnyStr) -> AnyStr:
    print(x + y)

f('one', b'two')

This correctly fails to typecheck because the second arg is bytes, but the error message is hard to follow:

test.py:6: error: Type argument 1 of "f" has incompatible value "object"
@JukkaL JukkaL added the bug mypy got something wrong label Oct 30, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented Oct 30, 2015

The error message is pretty confusing and this has came up before. I think Guido gave a talk about typing that happened to have this message as an example which was a little awkward as it's so opaque :-/

A better message would show all argument types and enumerate all the valid signatures. A similar thing should be done for overloaded functions.

Potential error message (we should iterate on this):

test.py:6: error: Invalid argument types for "f" (str, bytes)
test.py:6: note: Valid argument types for "f":
test.py:6: note:   (str, str)
test.py:6: note:   (bytes, bytes)

This gets a little complicated if there are keyword arguments, etc.

Maybe we could point to the definition of f as well.

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 14, 2016

What about including this in a milestone, now that we've been improving error messages? This must be one of the worst error messages users will commonly encounter.

@gvanrossum gvanrossum added this to the 0.4.x milestone Sep 14, 2016
@gvanrossum gvanrossum removed this from the 0.4.x milestone Mar 29, 2017
@quartox
Copy link
Contributor

quartox commented May 23, 2017

I will look into this.

@OddBloke
Copy link
Contributor

OddBloke commented Sep 2, 2017

For reference, this now outputs:

963.py:6: error: Value of type variable "AnyStr" of "f" cannot be "object"

which does direct the user towards the problem a little more; the confusing reference to "object" is still present.

@OddBloke
Copy link
Contributor

OddBloke commented Sep 2, 2017

Also, this:

from typing import AnyStr

class One(str):
    pass

class Two(str):
    pass

OneTwo = TypeVar('OneTwo', One, Two)

def g(x: OneTwo, y: OneTwo) -> None:
    print(x + y)

g(One('a'), Two('b'))

does not resolve to object as the invalid value:

963.py:14: error: Value of type variable "OneTwo" of "g" cannot be "str"

@ilevkivskyi ilevkivskyi self-assigned this Nov 16, 2017
@JukkaL JukkaL changed the title confusing error message when using AnyStr Confusing error message when using AnyStr May 18, 2018
@ilevkivskyi ilevkivskyi removed their assignment Dec 9, 2019
@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label Mar 19, 2022
@JelleZijlstra
Copy link
Member

Currently this outputs:

main.py:3: error: Missing return statement
main.py:6: error: Value of type variable "AnyStr" of "f" cannot be "Sequence[object]"

which is arguably worse. I think this is another entry for the topic-join-v-union Using join vs. using unions list: if we inferred a union instead of a join here, the error message would say something like Value of type variable "AnyStr" of "f" cannot be "Union[bytes, str]", which is much better.

For comparison pyright says:

Argument of type "Literal[b'two']" cannot be assigned to parameter "y" of type "AnyStr@f" in function "f"
  Type "bytes" is incompatible with constrained type variable "str"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high topic-join-v-union Using join vs. using unions topic-usability
Projects
None yet
Development

No branches or pull requests

7 participants