Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

Fixes #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: ... 
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: ... ```
hauntsaninja pushed a commit to hauntsaninja/typeshed that referenced this pull request Jan 7, 2021
Copy link
Collaborator

@JukkaL JukkaL left a 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]?

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__.

@hauntsaninja
Copy link
Collaborator Author

Thanks, fixed! Yeah, that's the overload I used to find these. You can also use the overload def getattr(__o: Any, name: str, __default: _T) -> Union[_T, Any]: ... for a slightly more aggressive change.

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)

@JukkaL JukkaL merged commit 0996c42 into python:master Jan 8, 2021
@hauntsaninja hauntsaninja deleted the fix branch January 18, 2021 00:47
ilevkivskyi pushed a commit that referenced this pull request Jan 19, 2021
Fixes #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: ... ``` Co-authored-by: hauntsaninja <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants