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

assert_type() not matching function type #15153

Closed
DiegoBaldassarMilleuno opened this issue Apr 28, 2023 · 9 comments · Fixed by #15258
Closed

assert_type() not matching function type #15153

DiegoBaldassarMilleuno opened this issue Apr 28, 2023 · 9 comments · Fixed by #15258
Labels
assert-type assert_type() bug mypy got something wrong topic-usability

Comments

@DiegoBaldassarMilleuno
Copy link

Bug Report

'assert_type()' refuses to match a function to its own type.

To Reproduce

from typing import assert_type, Callable
def myfunc(arg: int) -> None: pass
assert_type(myfunc, Callable[[int], None])

# Gist URL: https://gist.github.com/mypy-play/50dde8e891d971d823c4c6af2e639834
# Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=50dde8e891d971d823c4c6af2e639834

Expected Behavior

No error.

Actual Behavior

main.py:5: error: Expression is of type "Callable[[int], None]", not "Callable[[int], None]"  [assert-type]
Found 1 error in 1 file (checked 1 source file)

The error disappears if the function takes no arguments.

Your Environment

  • Mypy version used: both 1.2.0 and current master branch (commit 29cc561)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.11
@DiegoBaldassarMilleuno DiegoBaldassarMilleuno added the bug mypy got something wrong label Apr 28, 2023
@JelleZijlstra JelleZijlstra added the assert-type assert_type() label Apr 28, 2023
@erictraut
Copy link

The two types are not equivalent. The function myfunc accepts a keyword argument for a parameter named arg, but Callable[[int], None] accepts an argument by position only. If you add a positional-only parameter marker to myfunc, they will be equivalent.

def myfunc(arg: int, /) -> None:
    pass

@ikonst
Copy link
Contributor

ikonst commented Apr 28, 2023

What should we show in the error message, given there's no special form for this?

@AlexWaygood
Copy link
Member

@erictraut is correct, but the error message mypy gives here isn't particularly helpful. Let's keep this issue open to track improving the error message.

@erictraut
Copy link

FWIW, here's the error message that pyright emits:

"assert_type" mismatch: expected "(int) -> None" but received "(arg: int) -> None"

In pyright's error messages, I output a callable syntax that is based on the proposed (but unfortunately rejected) PEP 677. Not sure if this is something mypy maintainers would consider adopting.

@DiegoBaldassarMilleuno
Copy link
Author

I see, thank you very much for the clarification. The error message had me assuming it was a bug.

@JelleZijlstra
Copy link
Member

It's definitely an error message bug, since we mention the same string on both sides.

@madt2709
Copy link
Contributor

This has been fixed on the master branch. The error message now shows:

main.py:5: error: Expression is of type "Callable[[Arg(int, 'arg')], None]", not "Callable[[int], None]"  [assert-type]
Found 1 error in 1 file (checked 1 source file)

You can see this in the playground: https://mypy-play.net/?mypy=master&python=3.11&gist=50dde8e891d971d823c4c6af2e639834

@ikonst
Copy link
Contributor

ikonst commented May 17, 2023

Apparently fixed by #15184.

@AlexWaygood, can we close?

@AlexWaygood
Copy link
Member

Apparently fixed by #15184.

@AlexWaygood, can we close?

Doesn't look like a regression test was added for this specific case — fancy making a PR? :)

JelleZijlstra pushed a commit that referenced this issue May 18, 2023
Closes #15153.

This issue was already addressed in #15184; we're adding a regression
test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert-type assert_type() bug mypy got something wrong topic-usability
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants