Skip to content

Be more careful with sys.path editing in sitepkgs #5265

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

Merged
merged 1 commit into from
Jun 23, 2018

Conversation

gvanrossum
Copy link
Member

The sitepkgs.py script (support for PEP 561) was dropping sys.path[0], but it is also imported by mypy itself, and this broke the typed_ast import when run under Bazel. Bazel puts very few things on sys.path, and sys.path[0] is important -- it has the dependencies. I hope that the editing is only necessary when it's run as an independent script (through subprocess) by the PEP 561 code.

The sitepkgs.py script was dropping sys.argv[0], but it is also
imported by mypy itself, and this broke the typed_ast import when run
under Bazel.  I *hope* that the editing is only necessary when it's
run as an independent script (through subprocess) by the PEP 561 code.
@gvanrossum gvanrossum requested a review from emmatyping June 23, 2018 01:38
@gvanrossum
Copy link
Member Author

Also, why was this line introduced? It came in #5060 and the comment says # we don't want to pick up mypy.types but I have no idea how that comment relates to #5060. If it was about making tests pass, then something else must have changed, because if I take it out the tests still pass. (Unless it was about test flakes?)

@JelleZijlstra
Copy link
Member

I'm not sure of the exact context, but I think without this line Python (presumably 2.7) sometimes imported mypy.types as if it was the standard library types module (which obviously causes things to blow up).

@gvanrossum
Copy link
Member Author

Aha! You're right -- when run as a script, sys.path[0] is the mypy package directory. That makes me confident that this fix is right: it keeps the fix when it's run as a __main__ module, but drops it when imported as a submodule of mypy.

I also just ran the full test suite after completely deleting the fix, and indeed it got some errors in subprocess.

@emmatyping
Copy link
Member

Aha! You're right -- when run as a script, sys.path[0] is the mypy package directory. That makes me confident that this fix is right: it keeps the fix when it's run as a main module, but drops it when imported as a submodule of mypy.

Yes that is exactly why this was added. Thank you for adding this, it is probably a good idea bazel aside!

@gvanrossum gvanrossum merged commit 96f33a7 into python:master Jun 23, 2018
@gvanrossum gvanrossum deleted the fix-sitepkgs branch June 23, 2018 21:45
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

Successfully merging this pull request may close these issues.

3 participants