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

@sys.meta_path.append type error #5589

Closed
warsaw opened this issue Sep 7, 2018 · 5 comments
Closed

@sys.meta_path.append type error #5589

warsaw opened this issue Sep 7, 2018 · 5 comments

Comments

@warsaw
Copy link
Member

warsaw commented Sep 7, 2018

I'm not sure if this is a bug in Python or in mypy, or there is no bug at all. It seems a bit weird given the contents of sys.meta_path and my grep-fu must suck because I can't find where the signature is defined. Here's the code:

import sys


@sys.meta_path.append
class LostFinder:
    @staticmethod
    def find_spec(*args, **kws):
        return None

and the error:

$ mypy foo.py 
foo.py:4: error: Argument 1 to "append" of "list" has incompatible type "Type[LostFinder]"; expected "MetaPathFinder"

and the contents of sys.meta_path on a vanilla Python:

$ python3.7 -I -c "import sys; print(sys.meta_path)"
[<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
@msullivan
Copy link
Collaborator

The type of sys.meta_path is defined in the typeshed stub: https://github.com/python/typeshed/blob/master/stdlib/3/sys.pyi#L42

The class is defined here: https://github.com/python/typeshed/blob/master/stdlib/3/importlib/abc.pyi#L55

Subclassing from it should fix the error.

I don't think there is any bug here?

@msullivan
Copy link
Collaborator

(Feel free to reopen if you disagree)

@jaraco
Copy link
Member

jaraco commented Sep 8, 2018

Perhaps it's a bug in typeshed... and the spec should be more lenient, to allow for MetaPathFinder or Type[MetaPathFinder] or maybe any other class that duck types a MetaPathFinder (because even the stdlib <class '_frozen_importlib.BuiltinImporter'> is not an instance of MetaPathFinder.

@msullivan
Copy link
Collaborator

msullivan commented Sep 8, 2018

Oh, indeed, I missed that it contained classes and not the objects.

  1. It is definitely a typeshed bug that it doesn't allow Type[MetaPathFinder]
  2. Arguably typeshed should use a protocol too

@ilevkivskyi
Copy link
Member

Yes it is a typeshed bug, not mypy bug. @msullivan's suggestion should fix it in typeshed.

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

No branches or pull requests

4 participants