Skip to content

Commit 8ed16d1

Browse files
authored
stubgen: Don't generate Incomplete | None = None argument annotation (#19097)
Fixes #19096
1 parent 644a20c commit 8ed16d1

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

mypy/stubgen.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]:
565565
default = "..."
566566
if arg_.initializer:
567567
if not typename:
568-
typename = self.get_str_type_of_node(arg_.initializer, True, False)
568+
typename = self.get_str_type_of_node(arg_.initializer, can_be_incomplete=False)
569569
potential_default, valid = self.get_str_default_of_node(arg_.initializer)
570570
if valid and len(potential_default) <= 200:
571571
default = potential_default
@@ -1305,9 +1305,7 @@ def is_private_member(self, fullname: str) -> bool:
13051305
parts = fullname.split(".")
13061306
return any(self.is_private_name(part) for part in parts)
13071307

1308-
def get_str_type_of_node(
1309-
self, rvalue: Expression, can_infer_optional: bool = False, can_be_any: bool = True
1310-
) -> str:
1308+
def get_str_type_of_node(self, rvalue: Expression, *, can_be_incomplete: bool = True) -> str:
13111309
rvalue = self.maybe_unwrap_unary_expr(rvalue)
13121310

13131311
if isinstance(rvalue, IntExpr):
@@ -1327,9 +1325,7 @@ def get_str_type_of_node(
13271325
return "complex"
13281326
if isinstance(rvalue, NameExpr) and rvalue.name in ("True", "False"):
13291327
return "bool"
1330-
if can_infer_optional and isinstance(rvalue, NameExpr) and rvalue.name == "None":
1331-
return f"{self.add_name('_typeshed.Incomplete')} | None"
1332-
if can_be_any:
1328+
if can_be_incomplete:
13331329
return self.add_name("_typeshed.Incomplete")
13341330
else:
13351331
return ""

test-data/unit/stubgen.test

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ def g(b=-1, c=0): ...
3030
def f(a, b: int = 2) -> None: ...
3131
def g(b: int = -1, c: int = 0) -> None: ...
3232

33-
[case testDefaultArgNone]
33+
[case testFuncDefaultArgNone]
3434
def f(x=None): ...
3535
[out]
36-
from _typeshed import Incomplete
37-
38-
def f(x: Incomplete | None = None) -> None: ...
36+
def f(x=None) -> None: ...
3937

4038
[case testDefaultArgBool]
4139
def f(x=True, y=False): ...
@@ -1379,7 +1377,7 @@ async def f(a):
13791377
[out]
13801378
async def f(a) -> None: ...
13811379

1382-
[case testInferOptionalOnlyFunc]
1380+
[case testMethodDefaultArgNone]
13831381
class A:
13841382
x = None
13851383
def __init__(self, a=None):
@@ -1391,8 +1389,8 @@ from _typeshed import Incomplete
13911389

13921390
class A:
13931391
x: Incomplete
1394-
def __init__(self, a: Incomplete | None = None) -> None: ...
1395-
def method(self, a: Incomplete | None = None) -> None: ...
1392+
def __init__(self, a=None) -> None: ...
1393+
def method(self, a=None) -> None: ...
13961394

13971395
[case testAnnotationImportsFrom]
13981396
import foo
@@ -2618,32 +2616,29 @@ class A(metaclass=abc.ABCMeta):
26182616
@abc.abstractmethod
26192617
def x(self): ...
26202618

2621-
[case testClassWithNameIncompleteOrOptional]
2619+
[case testClassWithNameIncomplete]
26222620
Y = object()
26232621

2624-
def g(x=None): pass
2622+
def g():
2623+
yield 1
26252624

26262625
x = g()
26272626

26282627
class Incomplete:
26292628
pass
26302629

2631-
def Optional():
2632-
return 0
2633-
26342630
[out]
26352631
from _typeshed import Incomplete as _Incomplete
2632+
from collections.abc import Generator
26362633

26372634
Y: _Incomplete
26382635

2639-
def g(x: _Incomplete | None = None) -> None: ...
2636+
def g() -> Generator[_Incomplete]: ...
26402637

26412638
x: _Incomplete
26422639

26432640
class Incomplete: ...
26442641

2645-
def Optional(): ...
2646-
26472642
[case testExportedNameImported]
26482643
# modules: main a b
26492644
from a import C

0 commit comments

Comments
 (0)