pytest-dev / py

Python development support library (note: maintenance only)
MIT License
67 stars 106 forks source link

Use importlib to avoid module name clashes for pytest #203

Closed neumond closed 5 years ago

neumond commented 5 years ago

This allows simple modification for pytest --import-mode=importlib to completely avoid module naming issue if you have fresh python (>=3.5). This skips modifying sys.path, guessing package names, attempting to correctly import __init__.py and directories. This is more than enough for usual pytest workflow, e.g. if you have unimportable tests directory with files like test_something.py.

kxepal commented 5 years ago

This is very useful! Any chances to get this merged?

denis-trofimov commented 5 years ago

Moscow Python Beer Meetup report rules!

valignatev commented 5 years ago

This would be very helpful

sobolevn commented 5 years ago

This is an amazing feature!

neumond commented 5 years ago

screenshot from 2018-12-13 23-37-39 I've made additional checks to prove these changes actually work with pytest. As expected, it requires only adding "importlib" to --import-mode choices.

blueyed commented 5 years ago

@RonnyPfannschmidt What is your opinion on this?

RonnyPfannschmidt commented 5 years ago

this is a nice solution we can use as a temporary opt in, but im under the impression we are missing a deeper detail

however i don't have time in the near future to work deeply enough into the related topic to make a good case for those details

as such i believe a good way forward is to get this in, have it as opt-in in pytest an sort out the more detailed issues after the port to pathlib is complete

nicoddemus commented 5 years ago

We just need a CHANGELOG entry so we can merge this in. 👍

nicoddemus commented 5 years ago

Let's wait a day or two to get #207 and #215 in the next release as well. 👍

kxepal commented 5 years ago

:tada: Thank you all!

nicoddemus commented 5 years ago

Thanks again @neumond for the great PR!

blueyed commented 5 years ago

@neumond Do you plan adding creating a PR to add this to pytest (choices/help, doc)? (just stumbled across via py's docstring)

blueyed commented 5 years ago

btw: would it still possible to support relative imports? (ImportError: attempted relative import with no known parent package)

blueyed commented 5 years ago

Created https://github.com/pytest-dev/pytest/pull/5352.