Skip to content

stubtest treats constructors of native types as (self, *args, **kwargs). #17672

Open
@SquidDev

Description

@SquidDev

Bug Report

TLDR: If you have a zero-arg constructor of a native type, stubtest complains that stub does not have *args argument "args".

From what I understand, constructors of native types will always have a __textsignature__ of __init__($self, /, *args, **kwargs). Instead, the constructor's signature is actually stored on the type's __textsignature__.

However, stubtest never reads the type's signature, and so treats all native constructors as having a (self, *args, **kwargs) constructor. Most of the time this doesn't matter, as _verify_signature allows the stub's type signature to be more precise than the runtime one. However, if the stub's constructor takes no arguments, then we get an error.

To Reproduce
Create the following "foo.py" and "foo.pyi"

# In foo.py:
SomeType = object

# In foo.pyi:
class SomeType:
    def __init__(self): ...

Then run stubtest with stubtest foo.

Expected Behaviour

I would expect this to use the type's signature, and not produce any error.

Actual Behaviour

Instead, we get an error about a missing *args:

error: foo.SomeType.__init__ is inconsistent, stub does not have *args argument "args"
Stub: in file foo.pyi:2
None
Runtime:
def (self, /, *args, **kwargs)

Found 1 error (checked 1 module)

Your Environment

  • Mypy version used: 1.11.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.1012 on Linux (WSL)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions