Skip to content
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

Fix type errors stemming from getattr #9889

Merged
merged 4 commits into from Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/moduleinspect.py
Expand Up @@ -45,7 +45,7 @@ def get_package_properties(package_id: str) -> ModuleProperties:
package = importlib.import_module(package_id)
except BaseException as e:
raise InspectError(str(e)) from e
name = getattr(package, '__name__', None)
name = package.__name__
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think that this is safe, since all sorts of objects can be stuffed into sys.modules, and there's no guarantee that there's a __name__ attribute, as far as I can tell. I think it would be better to default to package_id as the name if there's no __name__.

file = getattr(package, '__file__', None)
path = getattr(package, '__path__', None) # type: Optional[List[str]]
if not isinstance(path, list):
Expand Down
2 changes: 1 addition & 1 deletion mypy/stubdoc.py
Expand Up @@ -202,7 +202,7 @@ def args_kwargs(signature: FunctionSig) -> bool:
return list(sorted(self.signatures, key=lambda x: 1 if args_kwargs(x) else 0))


def infer_sig_from_docstring(docstr: str, name: str) -> Optional[List[FunctionSig]]:
def infer_sig_from_docstring(docstr: Optional[str], name: str) -> Optional[List[FunctionSig]]:
"""Convert function signature to list of TypedFunctionSig

Look for function signatures of function in docstring. Signature is a string of
Expand Down