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

Possible bug in mypy when attribute and parameter share name on same line. #17177

Open
btimby opened this issue Apr 26, 2024 · 2 comments
Open
Labels
bug mypy got something wrong

Comments

@btimby
Copy link

btimby commented Apr 26, 2024

Bug Report

(A clear and concise description of what the bug is.)
I receive the following error from mypy:

api/serializers.py:10: error: Incompatible types in assignment (expression has type "CharField", base class "Field" defined the type as "Callable[..., Any] | str | None") [assignment]

The line in question is:

    source = serializers.CharField(source='source_name')

If I change the name of the attribute source to anything else, the error disappears. I think the attribute and parameter are getting mixed up. The error refers to the attribute assignment but reports the type Callable[..., Any] | str | None" which is correct for the parameter.

To Reproduce
I created a project to display this behavior.

https://github.com/btimby/mypy-django-issue

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)

Expected Behavior
I don't think I should receive and error.

Actual Behavior
I get an error.

Your Environment
Ubuntu linux, python 3.10.2

  • Mypy version used: 1.10.0
  • Mypy command-line flags: .
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
plugins =
    mypy_django_plugin.main

strict_optional = True

[mypy.plugins.django-stubs]
django_settings_module = issue.settings
  • Python version used: 3.10.2
@btimby btimby added the bug mypy got something wrong label Apr 26, 2024
@JelleZijlstra
Copy link
Member

Are you sure this isn't a bug in the Django plugin? It is developed independently from mypy itself.

@btimby
Copy link
Author

btimby commented Apr 26, 2024

No I am not. I don't think the problem is with the type annotation itself. It seems systemic to me, since renaming the attribute clears the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants