Skip to content

Commit

Permalink
Code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Oct 13, 2023
1 parent f0b0cf5 commit 2780a54
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def has_catchall_args(self) -> bool:
args = self.args
return (
len(args) == 2
and all(a.type in (None, "Any", "typing.Any") for a in args)
and all(a.type in (None, "object", "Any", "typing.Any") for a in args)
and args[0].is_star_arg()
and args[1].is_star_kwarg()
)

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

Expand Down
20 changes: 7 additions & 13 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,7 @@ def visit_mypy_file(self, o: MypyFile) -> None:
self.set_defined_names(find_defined_names(o))
self.referenced_names = find_referenced_names(o)
super().visit_mypy_file(o)
undefined_names = [name for name in self._all_ or [] if name not in self._toplevel_names]
if undefined_names:
if self._state != EMPTY:
self.add("\n")
self.add("# Names in __all__ with no definition:\n")
for name in sorted(undefined_names):
self.add(f"# {name}\n")
self.check_undefined_names()

def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
"""@property with setters and getters, @overload chain and some others."""
Expand Down Expand Up @@ -1479,13 +1473,13 @@ def generate_asts_for_modules(
def generate_stub_for_py_module(
mod: StubSource,
target: str,
*,
parse_only: bool = False,
inspect: bool = False,
include_private: bool = False,
export_less: bool = False,
include_docstrings: bool = False,
doc_dir: str = "",
*,
all_modules: list[str],
) -> None:
"""Use analysed (or just parsed) AST to generate type stub for single file.
Expand Down Expand Up @@ -1548,11 +1542,11 @@ def generate_stubs(options: Options) -> None:
generate_stub_for_py_module(
mod,
target,
options.parse_only,
options.inspect or mod in pyc_modules,
options.include_private,
options.export_less,
options.include_docstrings,
parse_only=options.parse_only,
inspect=options.inspect or mod in pyc_modules,
include_private=options.include_private,
export_less=options.export_less,
include_docstrings=options.include_docstrings,
doc_dir=options.doc_dir,
all_modules=all_module_names,
)
Expand Down
8 changes: 7 additions & 1 deletion mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_property_type(self, default_type: str | None, ctx: FunctionContext) -> s


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


def generate_stub_for_c_module(
Expand Down Expand Up @@ -417,6 +417,7 @@ def generate_module(self) -> None:
):
self._output.append("\n")
self._output.append(line + "\n")
self.check_undefined_names()

def is_skipped_attribute(self, attr: str) -> bool:
return (
Expand Down Expand Up @@ -557,6 +558,7 @@ def generate_function_stub(
if self.is_private_name(name, ctx.fullname) or self.is_not_in_all(name):
return

self.record_name(ctx.name)
default_sig = self.get_default_function_sig(obj, ctx)
inferred = self.get_signatures(default_sig, self.sig_generators, ctx)
self.process_inferred_sigs(inferred)
Expand Down Expand Up @@ -640,6 +642,7 @@ def generate_property_stub(
if self.is_private_name(name, ctx.fullname) or self.is_not_in_all(name):
return

self.record_name(ctx.name)
static = self.is_static_property(raw_obj)
readonly = self.is_property_readonly(raw_obj)
if static:
Expand Down Expand Up @@ -718,6 +721,8 @@ def generate_class_stub(self, class_name: str, cls: type, output: list[str]) ->
rw_properties: list[str] = []
ro_properties: list[str] = []
attrs: list[tuple[str, Any]] = []

self.record_name(class_name)
self.indent()

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

Expand Down
15 changes: 12 additions & 3 deletions mypy/stubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def add_import_line(self, line: str) -> None:
self._import_lines.append(line)

def get_imports(self) -> str:
"""Return the text for the stub."""
"""Return the import statements for the stub."""
imports = ""
if self._import_lines:
imports += "".join(self._import_lines)
Expand Down Expand Up @@ -601,7 +601,6 @@ def dedent(self) -> None:
"""Remove one level of indentation."""
self._indent = self._indent[:-4]

# move up
def record_name(self, name: str) -> None:
"""Mark a name as defined.
Expand All @@ -610,7 +609,6 @@ def record_name(self, name: str) -> None:
if self.is_top_level():
self._toplevel_names.append(name)

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

def check_undefined_names(self) -> None:
print(self._all_)
print(self._toplevel_names)
undefined_names = [name for name in self._all_ or [] if name not in self._toplevel_names]
if undefined_names:
if self._output:
self.add("\n")
self.add("# Names in __all__ with no definition:\n")
for name in sorted(undefined_names):
self.add(f"# {name}\n")

def get_signatures(
self,
default_signature: FunctionSig,
Expand Down
3 changes: 3 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,9 @@ x: int
class C:
def g(self): ...

# Names in __all__ with no definition:
# g

[case testIgnoreSlots]
class A:
__slots__ = ()
Expand Down

0 comments on commit 2780a54

Please sign in to comment.