Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

stubgen: Don't generateIncomplete | None = None argument annotation#19097

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 2 commits intopython:masterfromsrittau:stubgen-incomplete-none
May 16, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
10 changes: 3 additions & 7 deletionsmypy/stubgen.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -565,7 +565,7 @@ def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]:
default = "..."
if arg_.initializer:
if not typename:
typename = self.get_str_type_of_node(arg_.initializer,True,False)
typename = self.get_str_type_of_node(arg_.initializer,can_be_incomplete=False)
potential_default, valid = self.get_str_default_of_node(arg_.initializer)
if valid and len(potential_default) <= 200:
default = potential_default
Expand DownExpand Up@@ -1305,9 +1305,7 @@ def is_private_member(self, fullname: str) -> bool:
parts = fullname.split(".")
return any(self.is_private_name(part) for part in parts)

def get_str_type_of_node(
self, rvalue: Expression, can_infer_optional: bool = False, can_be_any: bool = True
) -> str:
def get_str_type_of_node(self, rvalue: Expression, *, can_be_incomplete: bool = True) -> str:
rvalue = self.maybe_unwrap_unary_expr(rvalue)

if isinstance(rvalue, IntExpr):
Expand All@@ -1327,9 +1325,7 @@ def get_str_type_of_node(
return "complex"
if isinstance(rvalue, NameExpr) and rvalue.name in ("True", "False"):
return "bool"
if can_infer_optional and isinstance(rvalue, NameExpr) and rvalue.name == "None":
return f"{self.add_name('_typeshed.Incomplete')} | None"
if can_be_any:
if can_be_incomplete:
return self.add_name("_typeshed.Incomplete")
else:
return ""
Expand Down
25 changes: 10 additions & 15 deletionstest-data/unit/stubgen.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,12 +30,10 @@ def g(b=-1, c=0): ...
def f(a, b: int = 2) -> None: ...
def g(b: int = -1, c: int = 0) -> None: ...

[casetestDefaultArgNone]
[casetestFuncDefaultArgNone]
def f(x=None): ...
[out]
from _typeshed import Incomplete

def f(x: Incomplete | None = None) -> None: ...
def f(x=None) -> None: ...

[case testDefaultArgBool]
def f(x=True, y=False): ...
Expand DownExpand Up@@ -1379,7 +1377,7 @@ async def f(a):
[out]
async def f(a) -> None: ...

[casetestInferOptionalOnlyFunc]
[casetestMethodDefaultArgNone]
class A:
x = None
def __init__(self, a=None):
Expand All@@ -1391,8 +1389,8 @@ from _typeshed import Incomplete

class A:
x: Incomplete
def __init__(self, a: Incomplete | None =None) -> None: ...
def method(self, a: Incomplete | None =None) -> None: ...
def __init__(self, a=None) -> None: ...
def method(self, a=None) -> None: ...

[case testAnnotationImportsFrom]
import foo
Expand DownExpand Up@@ -2618,32 +2616,29 @@ class A(metaclass=abc.ABCMeta):
@abc.abstractmethod
def x(self): ...

[casetestClassWithNameIncompleteOrOptional]
[casetestClassWithNameIncomplete]
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Testing forOptional seemed obsolete, since we don't usetyping.Optional anymore, so I removed it while I was here.

Y = object()

def g(x=None): pass
def g():
yield 1

x = g()

class Incomplete:
pass

def Optional():
return 0

[out]
from _typeshed import Incomplete as _Incomplete
from collections.abc import Generator

Y: _Incomplete

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

x: _Incomplete

class Incomplete: ...

def Optional(): ...

[case testExportedNameImported]
# modules: main a b
from a import C
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp