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)
JukkaL
left a comment
There was a problem hiding this comment.
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
| 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.
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: