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

Allow redefinition doesn't work #6959

Closed
Emerentius opened this issue Jun 8, 2019 · 6 comments
Closed

Allow redefinition doesn't work #6959

Emerentius opened this issue Jun 8, 2019 · 6 comments

Comments

@Emerentius
Copy link

  • 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.

redefine_me = 1
redefine_me = ""

I've used this mypy.ini:

[mypy]
python_version = 3.5
allow_redefinition = True

but I've also tried it with mypy --allow-redefinition, both with and without the ini-file.

  • What is the actual behavior/output?

testme.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")

  • 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.

@ilevkivskyi
Copy link
Member

This is expected behavior: --allow-redefinition only allows changing type after you have read the variable. For example:

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

@Emerentius
Copy link
Author

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.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 10, 2019

At least we should mention this in the documentation (#6963).

@abravalheri
Copy link

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 from the /tmp directory without any setup.cfg or mypi.ini around, and I got the following output:

% 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.

@ilevkivskyi
Copy link
Member

@abravalheri This is a separate issue being tracked in #6232 and #6233.

@abravalheri
Copy link

😄 Thank you very much @ilevkivskyi for pointing me that out, I will follow those issues.

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

No branches or pull requests

4 participants