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

Fix wrong version being found #31

Closed
flying-sheep opened this issue Oct 21, 2021 · 2 comments
Closed

Fix wrong version being found #31

flying-sheep opened this issue Oct 21, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@flying-sheep
Copy link
Contributor

@mtkennerly in #29 (comment):

I found an interesting side effect of this change when I ran the tests locally. I have Dunamai 1.5.5 installed globally, and the virtual environment has 1.6.0 because it's in pyproject.toml. The new get_version implementation finds 1.5.5 instead of the expected 1.6.0.

________________________ test__get_version__from_name _________________________

    def test__get_version__from_name() -> None:
>       assert get_version("dunamai") == Version(pkg_resources.get_distribution("dunamai").version)
E       AssertionError: assert Version(base='1.5.5', stage=None, revision=None, distance=0, commit=None, dirty=None, tagged_metadata=None) == Version(base='1.6.0', stage=None, revision=None, distance=0, commit=None, dirty=None, tagged_metadata=None)
E        +  where Version(base='1.5.5', stage=None, revision=None, distance=0, commit=None, dirty=None, tagged_metadata=None) = get_version('dunamai')
E        +  and   Version(base='1.6.0', stage=None, revision=None, distance=0, commit=None, dirty=None, tagged_metadata=None) = Version('1.6.0')
E        +    where '1.6.0' = dunamai 1.6.0 (d:\github\mtkennerly\dunamai\.venv\lib\site-packages).version
E        +      where dunamai 1.6.0 (d:\github\mtkennerly\dunamai\.venv\lib\site-packages) = <function get_distribution at 0x045AB780>('dunamai')
E        +        where <function get_distribution at 0x045AB780> = pkg_resources.get_distribution

tests\unit\test_dunamai.py:417: AssertionError

I'll look into this some more before making a new release.

@flying-sheep
Copy link
Contributor Author

I think if you want to debug this you need to step in with a debugger. I see this:

grafik

It only finds one PathFinder, which finds one distribution.

Could be that for you there’s multiple resolvers or there’s multiple dists found, hard to say.

Also we’d need to know what implementation is used in your case: For me, _discover_resolvers looks like it’d only search in sys.meta_path, so I think mine wouldn’t find packages in the wrong paths:

    @staticmethod
    def _discover_resolvers():
        """Search the meta_path for resolvers."""
        declared = (
            getattr(finder, 'find_distributions', None)
            for finder in sys.meta_path
            )
        return filter(None, declared)

@mtkennerly
Copy link
Owner

It looks like this was because of some clutter in the virtual environment, maybe from having tested multiple Poetry versions or from some other tool. I'm not exactly sure what happened, but I don't think it's a normal occurrence.

I tried modifying the affected test:

def test__get_version__from_name() -> None:
    import sys
    import importlib_metadata as ilm
    print("::::: sys.meta_path =", sys.meta_path)
    for finder in sys.meta_path:
        print(finder)
        declared = getattr(finder, "find_distributions", None)
        if declared:
            try:
                results = list(finder.find_distributions(ilm.DistributionFinder.Context(name='dunamai')))
                for (i, result) in enumerate(results):
                    print(f"  find_distributions[{i}].version = {result.version}")
                    print(f"  find_distributions[{i}].files = {result.files}")
                    # print(f"  find_distributions[{i}].metadata = {result.metadata}")
            except Exception as e:
                print(f"  error: {e}")
        else:
            print("  'find_distributions' not declared")
    assert get_version("dunamai") == Version(pkg_resources.get_distribution("dunamai").version)

Partial output:

$ poetry run pytest tests -s -k test__get_version__from_name
::::: sys.meta_path = [<_pytest.assertion.rewrite.AssertionRewritingHook object at 0x03867850>, <_virtualenv._Finder object at 0x01810330>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>, <six._SixMetaPathImporter object at 0x01879A30>, <importlib_metadata.MetadataPathFinder object at 0x033B8BB0>, <pkg_resources.extern.VendorImporter object at 0x03915BD0>]
<_pytest.assertion.rewrite.AssertionRewritingHook object at 0x03867850>
  'find_distributions' not declared
<_virtualenv._Finder object at 0x01810330>
  'find_distributions' not declared
<class '_frozen_importlib.BuiltinImporter'>
  'find_distributions' not declared
<class '_frozen_importlib.FrozenImporter'>
  'find_distributions' not declared
<class '_frozen_importlib_external.PathFinder'>
  'find_distributions' not declared
<six._SixMetaPathImporter object at 0x01879A30>
  'find_distributions' not declared
<importlib_metadata.MetadataPathFinder object at 0x033B8BB0>
  find_distributions[0].version = 1.6.0
  find_distributions[0].files = [PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai.pth'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Scripts\\dunamai'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Scripts\\dunamai.cmd'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.6.0.dist-info\\METADATA'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.6.0.dist-info\\INSTALLER'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.6.0.dist-info\\entry_points.txt'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.6.0.dist-info\\RECORD')]
  find_distributions[1].version = 1.7.0
  find_distributions[1].files = [PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai.pth'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Scripts\\dunamai'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Scripts\\dunamai.cmd'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.7.0.dist-info\\METADATA'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.7.0.dist-info\\INSTALLER'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.7.0.dist-info\\entry_points.txt'), PackagePath('D:\\GitHub\\mtkennerly\\dunamai\\.venv\\Lib\\site-packages\\dunamai-1.7.0.dist-info\\RECORD')]
  find_distributions[2].version = 1.5.4
  find_distributions[2].files = [PackagePath('README.md'), PackagePath('pyproject.toml'), PackagePath('setup.py'), PackagePath('dunamai/__init__.py'), PackagePath('dunamai/__main__.py'), PackagePath('dunamai.egg-info/PKG-INFO'), PackagePath('dunamai.egg-info/SOURCES.txt'), PackagePath('dunamai.egg-info/dependency_links.txt'), PackagePath('dunamai.egg-info/entry_points.txt'), PackagePath('dunamai.egg-info/requires.txt'), PackagePath('dunamai.egg-info/top_level.txt')]
<pkg_resources.extern.VendorImporter object at 0x03915BD0>
  'find_distributions' not declared

So it was finding multiple versions from these sources:

  • 1.6.0: ./.venv/Lib/site-packages/dunamai-1.6.0.dist-info
    • Files last modified today.
  • 1.7.0: ./.venv/Lib/site-packages/dunamai-1.7.0.dist-info
    • Files last modified today (I tried setting this version in pyproject.toml).
  • 1.5.4: ./dunamai.egg-info
    • Files last modified on 2021-01-20.
  • Also: ./.venv/Lib/site-packages/dunamai.pth (local path to repository)

If I do a fresh clone and poetry install (with Poetry 1.0.9) of Dunamai, then I only have ./dunamai.egg-info (with 1.6.0) and ./.venv/Lib/site-packages/dunamai.egg-link (one line with local path to repository and one line with .). Then the test passes, and the relevant output is:

<importlib_metadata.MetadataPathFinder object at 0x0374DF70>
  find_distributions[0].version = 1.6.0
  find_distributions[0].files = [PackagePath('LICENSE'), PackagePath('README.md'
), PackagePath('setup.py'), PackagePath('dunamai/__init__.py'), PackagePath('dun
amai/__main__.py'), PackagePath('dunamai/py.typed'), PackagePath('dunamai.egg-in
fo/PKG-INFO'), PackagePath('dunamai.egg-info/SOURCES.txt'), PackagePath('dunamai
.egg-info/dependency_links.txt'), PackagePath('dunamai.egg-info/entry_points.txt
'), PackagePath('dunamai.egg-info/requires.txt'), PackagePath('dunamai.egg-info/
top_level.txt')]

If I update the version in pyproject.toml and run poetry install, then ./dunamai.egg-info is updated accordingly, and the test still passes, without any of the extra ./.venv/Lib/site-packages/dunamai-<VERSION>.dist-info folders being created.

@mtkennerly mtkennerly added the bug Something isn't working label Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants