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

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]?

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

@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[stubgen] Crash on boost::python C extension
2 participants