Skip to content

Commit 2780a54

Browse files
committed
Code review updates
1 parent f0b0cf5 commit 2780a54

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

mypy/stubdoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ def has_catchall_args(self) -> bool:
8484
args = self.args
8585
return (
8686
len(args) == 2
87-
and all(a.type in (None, "Any", "typing.Any") for a in args)
87+
and all(a.type in (None, "object", "Any", "typing.Any") for a in args)
8888
and args[0].is_star_arg()
8989
and args[1].is_star_kwarg()
9090
)
9191

92-
def is_identity(self) -> bool:
92+
def is_catchall_signature(self) -> bool:
9393
"""Return if this signature is the catchall identity: (*args, **kwargs) -> Any"""
9494
return self.has_catchall_args() and self.ret_type in (None, "Any", "typing.Any")
9595

mypy/stubgen.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,7 @@ def visit_mypy_file(self, o: MypyFile) -> None:
434434
self.set_defined_names(find_defined_names(o))
435435
self.referenced_names = find_referenced_names(o)
436436
super().visit_mypy_file(o)
437-
undefined_names = [name for name in self._all_ or [] if name not in self._toplevel_names]
438-
if undefined_names:
439-
if self._state != EMPTY:
440-
self.add("\n")
441-
self.add("# Names in __all__ with no definition:\n")
442-
for name in sorted(undefined_names):
443-
self.add(f"# {name}\n")
437+
self.check_undefined_names()
444438

445439
def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
446440
"""@property with setters and getters, @overload chain and some others."""
@@ -1479,13 +1473,13 @@ def generate_asts_for_modules(
14791473
def generate_stub_for_py_module(
14801474
mod: StubSource,
14811475
target: str,
1476+
*,
14821477
parse_only: bool = False,
14831478
inspect: bool = False,
14841479
include_private: bool = False,
14851480
export_less: bool = False,
14861481
include_docstrings: bool = False,
14871482
doc_dir: str = "",
1488-
*,
14891483
all_modules: list[str],
14901484
) -> None:
14911485
"""Use analysed (or just parsed) AST to generate type stub for single file.
@@ -1548,11 +1542,11 @@ def generate_stubs(options: Options) -> None:
15481542
generate_stub_for_py_module(
15491543
mod,
15501544
target,
1551-
options.parse_only,
1552-
options.inspect or mod in pyc_modules,
1553-
options.include_private,
1554-
options.export_less,
1555-
options.include_docstrings,
1545+
parse_only=options.parse_only,
1546+
inspect=options.inspect or mod in pyc_modules,
1547+
include_private=options.include_private,
1548+
export_less=options.export_less,
1549+
include_docstrings=options.include_docstrings,
15561550
doc_dir=options.doc_dir,
15571551
all_modules=all_module_names,
15581552
)

mypy/stubgenc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def get_property_type(self, default_type: str | None, ctx: FunctionContext) -> s
135135

136136

137137
def is_pybind11_overloaded_function_docstring(docstring: str, name: str) -> bool:
138-
return docstring.startswith(f"{name}(*args, **kwargs)\n" + "Overloaded function.\n\n")
138+
return docstring.startswith(f"{name}(*args, **kwargs)\nOverloaded function.\n\n")
139139

140140

141141
def generate_stub_for_c_module(
@@ -417,6 +417,7 @@ def generate_module(self) -> None:
417417
):
418418
self._output.append("\n")
419419
self._output.append(line + "\n")
420+
self.check_undefined_names()
420421

421422
def is_skipped_attribute(self, attr: str) -> bool:
422423
return (
@@ -557,6 +558,7 @@ def generate_function_stub(
557558
if self.is_private_name(name, ctx.fullname) or self.is_not_in_all(name):
558559
return
559560

561+
self.record_name(ctx.name)
560562
default_sig = self.get_default_function_sig(obj, ctx)
561563
inferred = self.get_signatures(default_sig, self.sig_generators, ctx)
562564
self.process_inferred_sigs(inferred)
@@ -640,6 +642,7 @@ def generate_property_stub(
640642
if self.is_private_name(name, ctx.fullname) or self.is_not_in_all(name):
641643
return
642644

645+
self.record_name(ctx.name)
643646
static = self.is_static_property(raw_obj)
644647
readonly = self.is_property_readonly(raw_obj)
645648
if static:
@@ -718,6 +721,8 @@ def generate_class_stub(self, class_name: str, cls: type, output: list[str]) ->
718721
rw_properties: list[str] = []
719722
ro_properties: list[str] = []
720723
attrs: list[tuple[str, Any]] = []
724+
725+
self.record_name(class_name)
721726
self.indent()
722727

723728
class_info = ClassInfo(class_name, "", getattr(cls, "__doc__", None), cls)
@@ -802,6 +807,7 @@ def generate_variable_stub(self, name: str, obj: object, output: list[str]) -> N
802807
"""
803808
if self.is_private_name(name, f"{self.module_name}.{name}") or self.is_not_in_all(name):
804809
return
810+
self.record_name(name)
805811
type_str = self.strip_or_import(self.get_type_annotation(obj))
806812
output.append(f"{name}: {type_str}")
807813

mypy/stubutil.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def add_import_line(self, line: str) -> None:
571571
self._import_lines.append(line)
572572

573573
def get_imports(self) -> str:
574-
"""Return the text for the stub."""
574+
"""Return the import statements for the stub."""
575575
imports = ""
576576
if self._import_lines:
577577
imports += "".join(self._import_lines)
@@ -601,7 +601,6 @@ def dedent(self) -> None:
601601
"""Remove one level of indentation."""
602602
self._indent = self._indent[:-4]
603603

604-
# move up
605604
def record_name(self, name: str) -> None:
606605
"""Mark a name as defined.
607606
@@ -610,7 +609,6 @@ def record_name(self, name: str) -> None:
610609
if self.is_top_level():
611610
self._toplevel_names.append(name)
612611

613-
# move up
614612
def is_recorded_name(self, name: str) -> bool:
615613
"""Has this name been recorded previously?"""
616614
return self.is_top_level() and name in self._toplevel_names
@@ -636,6 +634,17 @@ def set_defined_names(self, defined_names: set[str]) -> None:
636634
# for the object during generation.
637635
self.add_name(f"{pkg}.{t}", require=False)
638636

637+
def check_undefined_names(self) -> None:
638+
print(self._all_)
639+
print(self._toplevel_names)
640+
undefined_names = [name for name in self._all_ or [] if name not in self._toplevel_names]
641+
if undefined_names:
642+
if self._output:
643+
self.add("\n")
644+
self.add("# Names in __all__ with no definition:\n")
645+
for name in sorted(undefined_names):
646+
self.add(f"# {name}\n")
647+
639648
def get_signatures(
640649
self,
641650
default_signature: FunctionSig,

test-data/unit/stubgen.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ x: int
747747
class C:
748748
def g(self): ...
749749

750+
# Names in __all__ with no definition:
751+
# g
752+
750753
[case testIgnoreSlots]
751754
class A:
752755
__slots__ = ()

0 commit comments

Comments
 (0)