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

-2 and --python-version 2.7 arguments differ in behaviour #5576

Closed
sqwishy opened this issue Sep 5, 2018 · 9 comments
Closed

-2 and --python-version 2.7 arguments differ in behaviour #5576

sqwishy opened this issue Sep 5, 2018 · 9 comments
Assignees
Labels
bug mypy got something wrong

Comments

@sqwishy
Copy link
Contributor

sqwishy commented Sep 5, 2018

This is on mypy 0.620 on Python 3.6.

My understanding is that the command arguments -2 and --python-version 2.7 are
supposed to do the same thing. But in infer_python_version_and_executable()
python_version is different on the given options and I think this affects how it finds a
value for python_executable.

If I'm understanding this correctly, I think it might be an issue with the arguments
having different dest values? -2 sets a constant on the wrong namespace?

> python3 -m pdb (which mypy) -2 .
> /home/sqwishy/.local/bin/mypy(4)<module>()
-> import re
(Pdb) b mypy/main.py:278
Breakpoint 1 at /home/sqwishy/.local/lib/python3.6/site-packages/mypy/main.py:278
(Pdb) c
> /home/sqwishy/.local/lib/python3.6/site-packages/mypy/main.py(278)infer_python_version_and_executable()
-> if special_opts.python_executable is not None and special_opts.python_version is not None:
(Pdb) pp vars(special_opts)
...
 'python_executable': None,
 'python_version': None,
 'silent_imports': False,
...
> python3 -m pdb (which mypy) --python-version 2.7 .
> /home/sqwishy/.local/bin/mypy(4)<module>()
-> import re
(Pdb) b mypy/main.py:278
Breakpoint 1 at /home/sqwishy/.local/lib/python3.6/site-packages/mypy/main.py:278
(Pdb) c
> /home/sqwishy/.local/lib/python3.6/site-packages/mypy/main.py(278)infer_python_version_and_executable()
-> if special_opts.python_executable is not None and special_opts.python_version is not None:
(Pdb) pp vars(special_opts)
...
 'python_executable': None,
 'python_version': (2, 7),
 'silent_imports': False,
...

I would expect special_opts.python_version to be the same in both cases.

@gvanrossum
Copy link
Member

Why are you digging in mypy's internals? Is there any externally observable behavior that differs?

@sqwishy
Copy link
Contributor Author

sqwishy commented Sep 5, 2018

I think the site-packages ends up being different. I could be wrong. I'm just not entirely sure how to show this. Sorry for being unclear.

@gvanrossum
Copy link
Member

gvanrossum commented Sep 5, 2018 via email

@sqwishy
Copy link
Contributor Author

sqwishy commented Sep 5, 2018

My problem was that I was getting Cannot find module named ... and it turned out I forgot a py.typed file because I can't read good. I was looking through mypy because I thought I was doing something wrong but wasn't sure what. Now that I know that...

I have two 2.7 packages, A and B. A just has one function with comment annotations and is installed into a 2.7 virtualenv with pip -e so there is an egg link. B imports A and calls that function with the wrong signature. When running mypy -2 (with Python 3.6) with my virtualenv sourced, I get Cannot find module named 'A'. but with mypy --python-version 2.7 I get what I would expect, a warning about using the wrong signature.

I hope that example makes more sense than what I wrote previously. I've also attached a gzipped
tar (mypy5576.tar.gz) with those testing packages (it's super small) in case that helps.

@gvanrossum gvanrossum assigned gvanrossum and ethanhs and unassigned gvanrossum Sep 5, 2018
@gvanrossum
Copy link
Member

@ethanhs Can you repro this?

@sqwishy
Copy link
Contributor Author

sqwishy commented Sep 5, 2018

It also seems if I write a mypy.ini that sets python_version: 2.7 it behaves like mypy -2.

I think because infer_python_version_and_executable() is reading python_version from special_opts but parse_config_file() set it on options? The result is that options.python_executable is set to a path to python 3 interpreter, not a python2.7 interpreter.

@gvanrossum gvanrossum mentioned this issue Sep 5, 2018
12 tasks
@gvanrossum gvanrossum added the bug mypy got something wrong label Sep 5, 2018
@gvanrossum
Copy link
Member

OK, I can definitely repro this. I essentially did this:

  • untar the tarball
  • create a Python 2 virtalenv and activate it
  • pip install -e a
  • cd b

Then I ran mypy two ways:

$ my --python-version 2.7 pkgb
pkgb/__main__.py:6: error: Argument 1 to "this_is_a_typed_function" has incompatible type "int"; expected "unicode"

That's expected. But with -2:

$ my -2 pkgb
pkgb/__main__.py:1: error: Cannot find module named 'pkga'
pkgb/__main__.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)

@sqwishy If you have an idea for a fix it may go quicker.

@gvanrossum
Copy link
Member

gvanrossum commented Sep 5, 2018 via email

@sqwishy
Copy link
Contributor Author

sqwishy commented Sep 14, 2018

Since the original pull request had some negative outcomes, I've reopened a new one to handle just the behavioural inconsistency between how mypy runs given the -2 and --python-version 2.7 command line arguments -- which is what this issue title speaks to.

I have opened a issue #5620 concerning how this problem relates to the configuration file as well, which I mentioned earlier here.

Hopefully this reduces and clarifies the scope of this stuff and makes things a little easier for you guys. Thanks for your patience.

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