Skip to content

stubgen generates an interface with invalid syntax for inheriting dataclass #12441

@ivtoskov

Description

@ivtoskov

Bug Report

The stubgen command generates an interface with invalid syntax for a data class that inherits from another class located in a different file.

To Reproduce

  1. Create a fresh virtual environment with Python >=3.7 and install mypy==0.941:
python3.9 -m venv venv
source venv/bin/activate
pip install -U pip
pip install mypy==0.941
  1. Create a file named a.py with the following content:
class A:
    pass
  1. Create a file named b.py in the same directory with the following content:
from dataclasses import dataclass
from a import A


@dataclass
class B(A):
    c: str
  1. Run stubgen b.py -o .
  2. Run python b.pyi

The generated b.pyi file has the following content:

from a import A

class B(A):
    c: str
    def __init__(self, *, c, **) -> None: ...

Notes:

  • The stub generation works correctly if we don't annotate the class with @dataclass.
  • The stub generation works correctly if the superclass A is in the same file.
  • The stub generation works correctly if B doesn't inherit from A.

Expected Behavior

The generated b.pyi file has valid Python syntax and the python command executes without errors.

Actual Behavior

The generated b.pyi file doesn't have valid Python syntax and the command python b.pyi produces the following error:

  File "/path/to/sandbox/b.pyi", line 5
    def __init__(self, *, c, **) -> None: ...
                               ^
SyntaxError: invalid syntax

Your Environment

  • Mypy version used: 0.941. Reproducible with 0.940 as well. Cannot reproduce with 0.931.
  • Mypy command-line flags: -o
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.0. Also reproduced with 3.7.12, 3.8.5 and 3.10.0. Cannot reproduce with 3.6.9.
  • Operating system and version: Ubuntu 20.04.3 LTS. Also reproduced with macOS Big Sur 11.5.2
  • pip list output for Python 3.9.0:
$ pip list
Package           Version
----------------- -------
mypy              0.941
mypy-extensions   0.4.3
pip               22.0.4
setuptools        49.2.1
tomli             2.0.1
typing_extensions 4.1.1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions