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
Conversation
Fixes python#9888 I applied the following patch to typeshed and ran self check: ``` +@overload +def getattr(__o: Any, name: str, __default: None) -> Optional[Any]: ... +@overload def getattr(__o: Any, name: str, __default: Any = ...) -> Any: ... ```
This found a couple issues in mypy (see python/mypy#9889)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getattr
is being a pain here. Maybe typeshed should use an overload so that getattr(o, a, None)
produces Optional[Any]
?
mypy/moduleinspect.py
Outdated
@@ -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__ |
There was a problem hiding this comment.
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__
.
Thanks, fixed! Yeah, that's the overload I used to find these. You can also use the overload Unfortunately, mypy_primer finds both potential typeshed changes somewhat problematic so I'm going to hold off (at least until we're back to having a release cycle) |
Fixes #9888
I applied the following patch to typeshed and ran self check: