-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Allow redefinition doesn't work #6959
Comments
This is expected behavior: x = 42
x = str(x) # this is OK because 'x' was used See also https://mypy.readthedocs.io/en/latest/command_line.html#miscellaneous-strictness-flags |
The docs say nothing about the read requirement. They say: "only redefinitions within the same block and nesting depth as the original definition are allowed". Even if this is expected to error, the message is misleading. |
At least we should mention this in the documentation (#6963). |
Hi guys, sorry to comment in this closed issue, but I ended up having a similar problem, I will try to describe it bellow: from typing import AnyStr
def force_bytes(xml: AnyStr) -> bytes:
if isinstance(xml, str):
xml = bytes(xml, "utf-8")
return xml I run this example directly with % mypy --allow-redefinition test.py
test.py:6: error: Incompatible types in assignment (expression has type "bytes", variable has type "str")
test.py:7: error: Incompatible return value type (got "str", expected "bytes")
Found 2 errors in 1 file (checked 1 source file) I tried running this on both (Python 3.6 + mypy 0.740) and (Python 3.8 + mypy from master) As you can see in the example, I am consuming the variable, but I think mypy gets a bit confused because of the if statement. Please let me know if you prefer that I open a different issue for this. Thank you very much for mypy anyway, it is great and keeps getting better. |
@abravalheri This is a separate issue being tracked in #6232 and #6233. |
😄 Thank you very much @ilevkivskyi for pointing me that out, I will follow those issues. |
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.
I've used this
mypy.ini
:but I've also tried it with
mypy --allow-redefinition
, both with and without the ini-file.What is the behavior/output you expect?
No warning
What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
0.701 and 3.6.7. Still happens with master.
The text was updated successfully, but these errors were encountered: