Skip to content

Catch TypeVisitor subclasses with unimplemented abstract methods #730

Closed

Description

@JamesGuthrie

Issue #638 was a pain to find because the root cause is an unimplemented abstract method which silently returns None and somewhere down the line causes type checking to fail. It would be better to check this explicitly. The following shows that this is done for one of the abstract methods:
https://github.com/JukkaL/mypy/blob/bbb2dd4f07f995a4b28b8044fa91aa97960fe192/mypy/types.py#L532. There are two potential solutions: 1) use abc's @AbstractMethod decorator, 2) raise an error directly in TypeVisitor.

  1. Ensures that future implementations and changes catch subclasses which do not implement the abstract methods, but requires adding implementations for all of the abstract methods to existing subclasses which are missing them. As there are a few, it would probably be simplest to raise an error there instead of providing the complete implementation, which raises the question: why not just use 2) straight off the bat?

@JukkaL Do you have a preferred solution?

Activity

  1. o11c commented on Jul 27, 2015

    @o11c
    Contributor

    I am suspicious of any visitors that can meaningfully work without full implementations.

    Perhaps there is a need for more specific visitors?

  2. JamesGuthrie commented on Jul 28, 2015

    @JamesGuthrie
    ContributorAuthor

    It appears as though there is a common pattern of unimplemented abstract methods (visit_star_type, visit_overloaded, visit_error_type, visit_erased_type), I figured that maybe they had been added after the initial implementation of all visitors, and don't appear in the test cases and as such haven't been caught. In total there are about 25 subclasses which do not implement all abstract methods, mostly it is either visit_star_type or visit_overloaded which are unimplemented.

  3. JukkaL commented on Aug 4, 2015

    @JukkaL
    Collaborator

    The current implementation is just sloppy, and is inherited from early pre-mypy days when there were no abstract methods.

    Using @abstractmethod is probably the best option, as missing implementations can then be caught statically. Trying to catch all of these errors using test cases is an uphill battle.

  4. JamesGuthrie commented on Aug 4, 2015

    @JamesGuthrie
    ContributorAuthor

    I've opened a PR to get the ball rolling.

    As mentioned in the PR, Subclasses with incomplete implementations are:

    EraseTypeVisitor (mypy/erasetype.py: 26)
        visit_ellipsis_type()
        visit_star_type()
    
    SameTypeVisitor(mypy/sametype.py: 29)
        visit_overloaded()
        visit_ellipsis_type()
        visit_star_type()
    
    ExpandTypeVisitor(mypy/expandtype.py: 35)
        visit_ellipsis_type()
        visit_star_type()
    
    SubtypeVisitor(mypy/subtypes.py: 44)
        visit_ellipsis_type()
        visit_star_type()
    
    TypeAnalyser(mypy/typeanal.py: 58)
        visit_error_type()
        visit_overloaded()
        visit_ellipsis_type()
        visit_star_type()
    
    TypeJoinVisitor(mypy/join.py: 70)
        visit_overloaded()
        visit_ellipsis_type()
        visit_star_type()
    
    TypeMeetVisitor(mypy/meet.py: 77)
        visit_ellipsis_type()
        visit_star_type()
    
    ConstraintBuilderVisitor(mypy/constraints.py: 125)
        visit_error_type()
        visit_overloaded()
        visit_ellipsis_type()
        visit_star_type()
        visit_type_list()
    
    TypeQuery(mypy/types.py: 760)
        visit_overloaded()
        visit_ellipsis_type()
    
    subclasses of TypeQuery:
        HasAnyQuery(mypy/stats.py: 223)
        HasAnyQuery2(mypy/stats.py: 241)
        ArgInferSecondpassQuery(mypy/checkexpr.py: 1375)
        HasTypeVarQuery(mypy/checkexpr.py: 1389)
        HasErasedComponentsQuery(mypy/checkexpr.py: 1402)
    
    TypeAnalyserPass3(mypy/typeanal.py: 234)
        visit_error_type()
        visit_overloaded()
        visit_ellipsis_type()
        visit_star_type()
        visit_type_list()
    
  5. 13 remaining items

  6. jbsiddall commented on Apr 13, 2018

    @jbsiddall

    Is there any update on this? mypy 0.580 is stil showing this error for code: for item in {int}: pass

  7. JelleZijlstra commented on Apr 13, 2018

    @JelleZijlstra
    Member

    Are you sure you're on the right issue? This one is talking about an internal problem in mypy as far as I can tell.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions