Skip to content

__getattr__ returning self type results in RecursionError in overloaded contexts #7243

@zero323

Description

@zero323

The following code (foo.pyi):

from typing import Any, Callable, overload

class T:
    def __getattr__(self, item: Any) -> T: ...

@overload
def f(f: Callable[..., Any], t: str = ...) -> Callable[...,  T]: ...
@overload
def f(f: str = ...) -> Callable[[Callable[..., Any]], Callable[..., T]]: ...

when used with current master

$ mypy --version                      
mypy 0.730+dev.aaad048cf6f88280f4295865882888b7721dacdb

with default options

mypy --show-traceback foo.py

results in RecursionError

Traceback (most recent call last):
  File "/home/zero323/anaconda3/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
...
RecursionError: maximum recursion depth exceeded while calling a Python object
foo.pyi:6: : note: use --pdb to drop into pdb

with repeated following calls:

  File "/path/to/anaconda3/lib/python3.7/site-packages/mypy/subtypes.py", line 152, in _is_subtype
    ignore_promotions=self.ignore_promotions)
  File "/path/to/anaconda3/lib/python3.7/site-packages/mypy/subtypes.py", line 95, in is_subtype
    ignore_promotions=ignore_promotions))
  File "/path/to/anaconda3/lib/python3.7/site-packages/mypy/types.py", line 667, in accept
    return visitor.visit_instance(self)
  File "/path/to/anaconda3/lib/python3.7/site-packages/mypy/subtypes.py", line 232, in visit_instance

The problem disappears if:

  • We remove __getattr__. Other dunders with the same signature, for example

    class T:
        def __getitem__(self, item: Any) -> T: ...
    

    seem to work fine.

  • We change return type of __getattr__, for example:

    class T:
         def __getattr__(self, item: Any) -> str: ...
    
  • We remove any of the overloaded f. Either

    def f(f: Callable[..., Any], t: str = ...) -> Callable[...,  T]: ...
    

    or

    def f(f: str = ...) -> Callable[[Callable[..., Any]], Callable[..., T]]: ...
    

    alone, works just fine.

  • We change return type of the first overloaded f, for example to:

    def f(f: Callable[..., Any], t: str = ...) -> Callable[...,  int]: ...
    

    or even other Generic of T:

    def f(f: Callable[..., Any], t: str = ...) -> List[T]: ..
    
  • We make t obligatory in both overloaded definitions, or remove it completely from the second one.

The same code causes segmentation fault with the latest PyPi release:

$ mypy --show-traceback foo.pyi
[1]    1530 segmentation fault  mypy --show-traceback foo.pyi

so it might be (?) related to #6579.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy