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

Ignoring types works differently in Python 3.8.13 than Python 3.9.13 #13920

Closed
nbro10 opened this issue Oct 19, 2022 · 2 comments
Closed

Ignoring types works differently in Python 3.8.13 than Python 3.9.13 #13920

nbro10 opened this issue Oct 19, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@nbro10
Copy link

nbro10 commented Oct 19, 2022

Bug Report

I'm trying to use # type: ignore to ignore certain false positives (errors) produced by mypy. In our project, I use multiple python versions, and, in particular, 3.8.13 and 3.9.13. I've noticed that mypy produces a different output for these 2 different versions of Python.

To Reproduce

Create the file main.py with the following contents

from pandas import DataFrame

data = DataFrame([[1, 2, 3], [3, 4, 5]], columns=["a", "b", "a"])
dup_columns_flags = data.columns.duplicated()

if dup_columns_flags.any():
    raise ValueError(
        f'data contains duplicated columns\n'  # type: ignore
        f'Columns: {data.columns.to_list()}\n'
        f'Duplicated columns: '
        f'{data.columns[dup_columns_flags].unique().to_list()}'
    )

Run mypy main.py with Python 3.8.13 and 3.9.13 and see that, in 3.9.13, the #type: ignore is not applicable to the whole fstring, but just the first line, while in 3.8.13 it seems to be applicable to the whole string. Of course, if I don't split the fstring into multiple lines, this doesn't happen.

In order to make mypy not report any error in 3.9.13, I need to do the following

from pandas import DataFrame

data = DataFrame([[1, 2, 3], [3, 4, 5]], columns=["a", "b", "a"])
dup_columns_flags = data.columns.duplicated()

if dup_columns_flags.any():
    raise ValueError(
        f'data contains duplicated columns\n'  
        f'Columns: {data.columns.to_list()}\n'  # type: ignore  
        f'Duplicated columns: '
        f'{data.columns[dup_columns_flags].unique().to_list()}'   # type: ignore  
    )

However, this does not work in Python 3.8.13. I would need also to place # type: ignore in front of the first line (like in the example above). So, in order to make this run in both versions, I need 3 comments, apparently

Expected Behavior

Mypy behaves in the same way in different python versions, if I am using the same dependencies

Actual Behavior

the opposite

Your Environment

  • Mypy version used: 0.950
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
...
# Configs in the pyproject.toml
[tool.mypy]
python_version = 3.8
warn_return_any = true
warn_unused_configs = true
...

The behaviour above doesn't change if I use python_version = 3.9

  • Python version used: 3.8.13 and 3.9.13
@nbro10 nbro10 added the bug mypy got something wrong label Oct 19, 2022
@erictraut
Copy link

I'm guessing that this is due to changes in the AST between Python 3.8 and 3.9.

In any case, I'm not able to repro the original problem even with older versions of mypy and Python 3.8 or Python 3.9. I'm not sure why a # type: ignore would be necessary here.

@hauntsaninja
Copy link
Collaborator

If Python AST has changed location information, this is a won't fix for mypy.

A common issue in cases like this is use of the --warn-unused-ignore setting, in which case you can use #8823 (comment)

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
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

3 participants