Skip to content

Fail fast if site-packages are in the path #5301

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 3 commits into from
Jun 30, 2018

Conversation

emmatyping
Copy link
Member

@emmatyping emmatyping commented Jun 29, 2018

With my refactoring in #5256, I realized that it would be pretty straightforward to give some nicer warnings if site-packages were in any of the PATHs.

mypy/build.py Outdated
package_path = tuple(_get_site_packages_dirs(options.python_executable))
for site in package_path:
if site in mypypath:
print("site-packages is in the MYPYPATH. Please remove it.", file=sys.stderr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there may be multiple site-packages dirs (else why the outer loop :-) it would be helpful to include the offending site value in the messages.

mypy/build.py Outdated
print("site-packages is in the PYTHONPAT. Please change directory so it is not.",
file=sys.stderr)
elif site in lib_path:
print("this should never, ever happen", file=sys.stderr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a capital letter and a trailing period. Also why should this never happen? And if if can't happen perhaps it should be an assert?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should never happen because the path to the typeshed stubs should never include a site-package directory. I will change it to an assert.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being so nitpicky.

mypy/build.py Outdated
else:
# this site not in any of the other paths, we are safe.
continue
sys.exit(1)
return SearchPaths(tuple(reversed(python_path)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel there ought to be a blank line here (like you had before the last commit).

mypy/build.py Outdated
else:
# this site not in any of the other paths, we are safe.
continue
sys.exit(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it's better to have two sys.exit(1) calls, one after each print(). Then you don't need to else: continue bit which feels a bit odd.

package_path = tuple(_get_site_packages_dirs(options.python_executable))
for site_dir in package_path:
assert site_dir not in lib_path
if site_dir in mypypath:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are of course many variant spellings of the same path that won't be caught by this check. I don't think it's necessary to worry about that (normalizing the paths feels kind of expensive), but we shouldn't feel too complacent. :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it won't be perfect, but I think it will catch and help debug some more common cases, as well as avoid crashes in those circumstances.

@emmatyping
Copy link
Member Author

Sorry for being so nitpicky.

Not at all, code quality matters. I appreciate constructive feedback, no matter how small :)

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG now!

@sbrunner
Copy link

sbrunner commented Jul 16, 2018

Looks to be related to this pull request, if I run:

MYPYPATH=/usr/local/lib/python3.6/dist-packages mypy --disallow-untyped-defs --strict-optional --follow-imports skip my_package

I get

/usr/local/lib/python3.6/dist-packages is in the MYPYPATH. Please remove it.

And if I run

mypy --disallow-untyped-defs --strict-optional --follow-imports skip commons/c2cgeoportal_commons

I get

commons/c2cgeoportal_commons/config.py:29: error: Cannot find module named 'c2c'

commons/c2cgeoportal_commons/config.py:29: error: Cannot find module named 'c2c.template'

commons/c2cgeoportal_commons/models/__init__.py:33: error: No library stub file for module 'sqlalchemy'

commons/c2cgeoportal_commons/models/__init__.py:33: note: (Stub files are from https://github.com/python/typeshed)

commons/c2cgeoportal_commons/models/__init__.py:33: error: No library stub file for module 'sqlalchemy.orm'

commons/c2cgeoportal_commons/models/__init__.py:34: error: No library stub file for module 'sqlalchemy.ext'

commons/c2cgeoportal_commons/models/__init__.py:34: error: No library stub file for module 'sqlalchemy.ext.declarative'

commons/c2cgeoportal_commons/models/__init__.py:35: error: No library stub file for module 'sqlalchemy.ext.declarative.api'

commons/c2cgeoportal_commons/models/__init__.py:37: error: Cannot find module named 'pyramid.i18n'

commons/c2cgeoportal_commons/__init__.py:4: error: Cannot find module named 'pyramid.config'

commons/c2cgeoportal_commons/__init__.py:4: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)

commons/c2cgeoportal_commons/models/sqlalchemy.py:31: error: No library stub file for module 'sqlalchemy.engine'

commons/c2cgeoportal_commons/models/sqlalchemy.py:32: error: No library stub file for module 'sqlalchemy.types'

commons/c2cgeoportal_commons/models/main.py:35: error: Cannot find module named 'papyrus.geo_interface'

commons/c2cgeoportal_commons/models/main.py:36: error: No library stub file for module 'sqlalchemy'

commons/c2cgeoportal_commons/models/main.py:37: error: No library stub file for module 'sqlalchemy.types'

commons/c2cgeoportal_commons/models/main.py:40: error: No library stub file for module 'sqlalchemy.schema'

commons/c2cgeoportal_commons/models/main.py:41: error: No library stub file for module 'sqlalchemy.orm'

commons/c2cgeoportal_commons/models/main.py:42: error: Cannot find module named 'geoalchemy2'

commons/c2cgeoportal_commons/models/main.py:43: error: Cannot find module named 'geoalchemy2.shape'

commons/c2cgeoportal_commons/models/main.py:46: error: Cannot find module named 'zope'

commons/c2cgeoportal_commons/models/main.py:46: error: Cannot find module named 'zope.event'

commons/c2cgeoportal_commons/models/main.py:47: error: Cannot find module named 'colander'

commons/c2cgeoportal_commons/models/main.py:48: error: Cannot find module named 'deform.widget'

commons/c2cgeoportal_commons/models/main.py:49: error: Cannot find module named 'c2cgeoform.ext'

commons/c2cgeoportal_commons/testing/__init__.py:3: error: Cannot find module named 'transaction'

commons/c2cgeoportal_commons/testing/__init__.py:4: error: Cannot find module named 'zope'

commons/c2cgeoportal_commons/testing/__init__.py:4: error: Cannot find module named 'zope.sqlalchemy'

commons/c2cgeoportal_commons/testing/__init__.py:5: error: No library stub file for module 'sqlalchemy'

commons/c2cgeoportal_commons/testing/__init__.py:6: error: No library stub file for module 'sqlalchemy.engine'

commons/c2cgeoportal_commons/testing/__init__.py:7: error: No library stub file for module 'sqlalchemy.orm'

commons/c2cgeoportal_commons/models/static.py:37: error: No library stub file for module 'sqlalchemy'

commons/c2cgeoportal_commons/models/static.py:38: error: No library stub file for module 'sqlalchemy.types'

commons/c2cgeoportal_commons/models/static.py:40: error: Cannot find module named 'colander'

commons/c2cgeoportal_commons/models/static.py:41: error: Cannot find module named 'deform.widget'

commons/c2cgeoportal_commons/models/static.py:42: error: Cannot find module named 'c2cgeoform.ext'

commons/c2cgeoportal_commons/testing/initializedb.py:5: error: Cannot find module named 'transaction'

commons/c2cgeoportal_commons/testing/initializedb.py:8: error: Cannot find module named 'pyramid.paster'

commons/c2cgeoportal_commons/testing/initializedb.py:10: error: Cannot find module named 'pyramid.scripts.common'

commons/c2cgeoportal_commons/testing/initializedb.py:11: error: No library stub file for module 'sqlalchemy.engine'

commons/c2cgeoportal_commons/testing/initializedb.py:12: error: No library stub file for module 'sqlalchemy.orm'

commons/c2cgeoportal_commons/testing/initializedb.py:13: error: No library stub file for module 'alembic'

commons/c2cgeoportal_commons/lib/email_.py:38: error: Cannot find module named 'pyramid.httpexceptions'

In one word he didn't find anything ...

Did I something wrong?

@JelleZijlstra
Copy link
Member

Try --ignore-missing-imports.

@sbrunner
Copy link

If I add that It pass but it nearly didn't check anything...

@emmatyping emmatyping deleted the clean-site branch July 17, 2018 05:40
@emmatyping
Copy link
Member Author

Hi, unfortunately, unless a package is marked as type safe, we do not inspect it. You may want to read about this in the docs: https://mypy.readthedocs.io/en/latest/installed_packages.html. If you have further questions, feel free to drop by our Gitter chat.

@sbrunner
Copy link

OK thanks @ethanhs :-)

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.

4 participants