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

Stubgen error when using extra param in SubClass definition #17661

Closed
bramstroker opened this issue Aug 11, 2024 · 3 comments · Fixed by #17663
Closed

Stubgen error when using extra param in SubClass definition #17661

bramstroker opened this issue Aug 11, 2024 · 3 comments · Fixed by #17663
Assignees
Labels

Comments

@bramstroker
Copy link

Bug Report

stub generation for Home Assistant is broken.

To Reproduce

Stubgen fails on this file:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/recorder/migration.py

This commit, which I found using git bisect
home-assistant/core@a22e12d

stubgen migration.py

I simplified as much as possible to have minimum reproduction code.
It's caused by the extra parameter pass in subclass definition, I'm not sure which python language construct this is and when it was added. But stubgen goes out on it.

Reproduction code:

class MyAbstractClass(ABC):
    def __init_subclass__(cls, param: int) -> None:
        cls.param = param

class SubClass(MyAbstractClass, param=10):
    def _apply_update(self) -> None:
        pass

Expected Behavior

Stubs are correctly generated without an error.

Actual Behavior

Traceback (most recent call last):
  File "/Users/bram/.pyenv/versions/3.12.1/bin/stubgen", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "mypy/stubgen.py", line 1895, in main
    generate_stubs(options)
  File "mypy/stubgen.py", line 1703, in generate_stubs
    generate_stub_for_py_module(
  File "mypy/stubgen.py", line 1673, in generate_stub_for_py_module
    mod.ast.accept(gen)
  File "mypy/nodes.py", line 372, in accept
    return visitor.visit_mypy_file(self)
  File "mypy/stubgen.py", line 466, in visit_mypy_file
    super().visit_mypy_file(o)
  File "mypy/traverser.py", line 116, in visit_mypy_file
    d.accept(self)
  File "mypy/nodes.py", line 1183, in accept
    return visitor.visit_class_def(self)
  File "mypy/stubgen.py", line 729, in visit_class_def
    base_types = self.get_base_types(o)
  File "mypy/stubgen.py", line 807, in get_base_types
    base_types.append(f"{name}={value.accept(p)}")
TypeError: str object expected; got None

Your Environment

  • Mypy version used: 1.11
  • Python version used: 3.12
@sobolevn
Copy link
Member

This only happens on compiled version of mypy, because value.accept(AliasPrinter()) returns None which breaks mypyc's expectation to receive a str instance for fstring formatting.

Looks like AliasPrinter should be extended to support literals.

@sobolevn
Copy link
Member

Simple test case to reproduce this problem:

[case testClassInheritanceWithKeywords]
class Test(Whatever, a=1, b='b', c=True, d=1.5, e=None): ...
[out]
class Test(Whatever, a=1, b='b', c=True, d=1.5, e=None): ...

Produces:

____________________________ testClassInheritanceWithKeywords _____________________________
[gw10] darwin -- Python 3.11.9 /Users/sobolev/Desktop/mypy/.venv/bin/python3
data: /Users/sobolev/Desktop/mypy/test-data/unit/stubgen.test:4409:
Failed: Invalid output (/Users/sobolev/Desktop/mypy/test-data/unit/stubgen.test, line 4409)
---------------------------------- Captured stderr call -----------------------------------
Expected:
  class Test(Whatever, a=1, b='b', c=True, d=1.5, e=None): ... (diff)
Actual:
  class Test(Whatever, a=None, b='b', c=True, d=None, e=None): ... (diff)

Alignment of first line difference:
  E: class Test(Whatever, a=1, b='b', c=True, d=1.5, e=None): ...
  A: class Test(Whatever, a=None, b='b', c=True, d=None, e=None): ...
                            ^

@bramstroker
Copy link
Author

@sobolevn Awesome, thanks very much for the quick resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants