Skip to content

types.ModuleType should define __getattr__ that returns Any #2147

Closed

Description

@ilevkivskyi

This will avoid false positives in several cases.

Activity

  1. added a commit that references this issue on Jun 12, 2018
    ba2cabd
  2. JelleZijlstra commented on Jun 12, 2018

    @JelleZijlstra
    Member

    I am trying this in master...JelleZijlstra:modulegetattr, but this produces some errors in mypy's test suite: https://travis-ci.org/JelleZijlstra/typeshed/jobs/391103559.

    It seems that this change makes mypy thinks that any module has any attribute, while what we want is that specific module have a known set of attributes and only variables that are just typed as ModuleType have any attribute. This will likely require changes to mypy itself.

  3. ilevkivskyi commented on Jun 12, 2018

    @ilevkivskyi
    MemberAuthor

    Hm, yes, it looks like we have three tests that explicitly check for Module has no attribute "y". So probably this will need some adjustments in mypy (like not using __getattr__ when types.ModuleType is itself a fallback).

  4. gvanrossum commented on Jun 12, 2018

    @gvanrossum
    Member
  5. ilevkivskyi commented on Jun 12, 2018

    @ilevkivskyi
    MemberAuthor

    TBH, I already forgot why I wanted this :-) Most likely this is because of some limitations of type inference in mypy, when a list of modules is inferred with too wide type, or when people want to patch modules in a function.

    However, if we will allow modules as subtypes of protocols (I promised to make a PR and it is still on my TODO list) this will solve all such problems, so maybe we can just abandon this idea? (I vote for protocols.)

  6. gvanrossum commented on Jun 12, 2018

    @gvanrossum
    Member

    A typical example would be

    import a, b, c  # It so happens that each defines a method foo()
    mods = [a, b, c]
    for mod in mods:
        mod.foo()
    

    IIRC there was a tracker issue (maybe several) about this and I also believe I've seen it in Dropbox internal code.

  7. ilevkivskyi commented on Jun 13, 2018

    @ilevkivskyi
    MemberAuthor

    Yes, then the protocol solution (which doesn't work yet) would be to just give an explicit annotation to mods:

    class Foo(Protocol):
        def foo(self) -> None: ...
    
    import a, b, c  # It so happens that each defines a method foo()
    mods: List[Foo] = [a, b, c]  # OK
    for mod in mods:
        mod.foo()
  8. JelleZijlstra commented on Jun 14, 2018

    @JelleZijlstra
    Member

    So it sounds like we shouldn't make any change in typeshed.

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