Skip to content

handle arg=None in stubgenc #18768

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

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
handle arg=None in stubgenc
  • Loading branch information
asottile committed Mar 7, 2025
commit d61c049fcc88671a3abe10dc7f07ca3fc667687d
20 changes: 12 additions & 8 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import enum
import glob
import importlib
import inspect
Expand Down Expand Up @@ -211,6 +212,9 @@ def __get__(self) -> None: # noqa: PLE0302
pass


_Missing = enum.Enum("_Missing", "VALUE")


class InspectionStubGenerator(BaseStubGenerator):
"""Stub generator that does not parse code.

Expand Down Expand Up @@ -310,12 +314,12 @@ def get_annotation(key: str) -> str | None:

# Add the arguments to the signature
def add_args(
args: list[str], get_default_value: Callable[[int, str], object | None]
args: list[str], get_default_value: Callable[[int, str], object | _Missing]
) -> None:
for i, arg in enumerate(args):
# Check if the argument has a default value
default_value = get_default_value(i, arg)
if default_value is not None:
if default_value is not _Missing.VALUE:
if arg in annotations:
argtype = annotations[arg]
else:
Expand All @@ -330,11 +334,11 @@ def add_args(
else:
arglist.append(ArgSig(arg, get_annotation(arg), default=False))

def get_pos_default(i: int, _arg: str) -> Any | None:
def get_pos_default(i: int, _arg: str) -> Any | _Missing:
if defaults and i >= len(args) - len(defaults):
return defaults[i - (len(args) - len(defaults))]
else:
return None
return _Missing.VALUE

add_args(args, get_pos_default)

Expand All @@ -345,11 +349,11 @@ def get_pos_default(i: int, _arg: str) -> Any | None:
elif kwonlyargs:
arglist.append(ArgSig("*"))

def get_kw_default(_i: int, arg: str) -> Any | None:
if kwonlydefaults:
return kwonlydefaults.get(arg)
def get_kw_default(_i: int, arg: str) -> Any | _Missing:
if kwonlydefaults and arg in kwonlydefaults:
return kwonlydefaults[arg]
else:
return None
return _Missing.VALUE

add_args(kwonlyargs, get_kw_default)

Expand Down
24 changes: 24 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,30 @@ class TestClassVariableCls:
assert_equal(gen.get_imports().splitlines(), ["from typing import ClassVar"])
assert_equal(output, ["class C:", " x: ClassVar[int] = ..."])

def test_generate_c_type_none_default(self) -> None:
class TestClass:
def test(self, arg0=1, arg1=None) -> None: # type: ignore[no-untyped-def]
pass

output: list[str] = []
mod = ModuleType(TestClass.__module__, "")
gen = InspectionStubGenerator(mod.__name__, known_modules=[mod.__name__], module=mod)
gen.is_c_module = False
gen.generate_function_stub(
"test",
TestClass.test,
output=output,
class_info=ClassInfo(
self_var="self",
cls=TestClass,
name="TestClass",
docstring=getattr(TestClass, "__doc__", None),
),
)
assert_equal(
output, ["def test(self, arg0: int = ..., arg1: Incomplete | None = ...) -> None: ..."]
)

def test_non_c_generate_signature_with_kw_only_args(self) -> None:
class TestClass:
def test(
Expand Down