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
Fix assertion rewriting module detection for egg dists #6313
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments:
I wonder if it is not possible to unify the two cases by using importlib_metadata
a bit more? Maybe by getting the file name using file.locate()
instead of str(file)
in _mark_plugins_for_rewrite()
, or maybe some other way - I'm not really familiar with that library.
Is there a reason not to just strip to at most the two last components (so it catches foo.py
and foo/__init__.py
) instead of stripping one component at a time and trying that? So source/python/bar/__init__.py
-> bar/__init__.py
instead of source/python/bar/__init__.py
-> python/bar/__init__.py
-> bar/__init__.py
.
Does the function need to handle subpackages, e.g. pytest_mock.foo._version
? If such a file existed, wouldn't it conflict with pytest_mock._version
? Or maybe subpackages are considered different "distributions"?
Good point, that's something that looks like worth pursuing. @asottile (which knows a lot more about
All good points, the original intent was to only mark for rewrite the top level package, because But on second thought, perhaps we don't need to be so nit-picky here and just add all modules/packages we find? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm of the opinion that we should not support eggs -- and I don't think this even does add egg support as they will not pass the exists(...)
check later on when looking to rewrite the actual files
maybe I don't understand the issue completely 🤔
if not seen_some: | ||
# at this point we did not find any packages or modules suitable for assertion | ||
# rewriting, so we try again by stripping the first path component (to account for | ||
# "src" based source trees for example) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not do this arbitrarily -- src
is just a use of package_dir
but it could be anywhere or any number of directories deep
perhaps a better approach would be to use sys.path
to determine whether they are modules or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not do this arbitrarily -- src is just a use of package_dir but it could be anywhere or any number of directories deep
That's just a comment, we are not hard-coding src
anywhere... we do strip the first directory (if we don't find a suitable mode/package) and try again in the next iteration.
https://github.com/pytest-dev/pytest-mock/issues/167 | ||
""" | ||
package_files = list(package_files) | ||
seen_some = False | ||
for fn in package_files: | ||
is_simple_module = "/" not in fn and fn.endswith(".py") | ||
is_package = fn.count("/") == 1 and fn.endswith("__init__.py") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm the existing code seems to be a pre-PEP420 world, I wonder if we could update this to not be so dependent on __init__.py
files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, pytest itself doesn't seem to support this and AFAIR many tools were struggling to add support to this as well...
though it is interesting that |
That was my initial reaction as well, but |
makes sense, we can probably iterate |
I'm not sure what you mean exactly, but I think whatever you think is appropriate here can be done; this code dates from our 2016 sprint, if there are improvements to be done to it we should. Can you post an example of using |
here's an example:
though hmmm, that file doesn't show up for editable installs so now I'm just confused 🤔 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm can I access that contents through importlib-metadata, or only by accessing the filesystem directly? |
I think we can go with the PR as is: it solves the existing problem without incurring additional overhead; we can improve this later if we find a more elegant way to accomplish this. 👍 |
e8fa03f
to
c7f9fda
Compare
Backported in #6368 |
This change causes a new warning in one of our projects, that uses
As a result this warning is now triggered
which is probably harmless, but still confusing. I am not familiar with the details of python packages ; is there a problem with how this specific dependency is packaged ? |
yeah they probably should not use |
Fix #6301