Skip to content

@sys.meta_path.append type error #5589

Closed

Description

@warsaw

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'>]

Activity

  1. msullivan commented on Sep 8, 2018

    @msullivan
    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?

  2. msullivan commented on Sep 8, 2018

    @msullivan
    Collaborator

    (Feel free to reopen if you disagree)

  3. jaraco commented on Sep 8, 2018

    @jaraco
    Member

    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.

  4. msullivan commented on Sep 8, 2018

    @msullivan
    Collaborator

    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
  5. ilevkivskyi commented on Sep 8, 2018

    @ilevkivskyi
    Member

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

  6. added a commit that references this issue on Sep 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions