Skip to content

builtins: add a getattr overload for bool #5518

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

Merged
merged 2 commits into from
May 29, 2021

Conversation

hauntsaninja
Copy link
Collaborator

As brought up in #5516

Alternatives include:

  • Use another type var that has a value restriction
  • Doing something fancy with Protocols that have a bool that returns
    a Literal (which may not work)
  • Doing nothing

As brought up in python#5516

Alternatives include:
- Use another type var that has a value restriction
- Doing something fancy with Protocols that have a __bool__ that returns
  a Literal (which may not work)
- Doing nothing
@hauntsaninja hauntsaninja changed the title builtins: add an overload for bool builtins: add a getattr overload for bool May 21, 2021
@github-actions

This comment has been minimized.

1 similar comment
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pytest (https://github.com/pytest-dev/pytest.git)
- src/_pytest/config/__init__.py:1050: error: Argument 3 to "getattr" has incompatible type "bool"; expected "Optional[Namespace]"  [arg-type]

tornado (https://github.com/tornadoweb/tornado.git)
- tornado/auth.py:323: error: Argument 3 to "getattr" has incompatible type "bool"; expected "Optional[str]"

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

Is there a reason to copy/paste the change to Python 2 stubs? I think they are already good enough after #5516.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Adding this for Python 2 also seems harmless

@JelleZijlstra JelleZijlstra merged commit 04fb7ce into python:master May 29, 2021
@hauntsaninja hauntsaninja deleted the getattrbool branch June 1, 2021 05:31
JukkaL added a commit to JukkaL/typeshed that referenced this pull request Nov 22, 2021
These silence errors about missing type annotations for calls
like these:

```
x = getattr(o, 'a', [])
y = getattr(o, 'b', {})
```

This is a generalization of python#5518.
JukkaL added a commit that referenced this pull request Nov 22, 2021
These silence errors about missing type annotations for calls
like these:

```
x = getattr(o, 'a', [])
y = getattr(o, 'b', {})
```

This is basically a generalization of #5518 and other overloads we already
have.

This works around python/mypy#11572. I encountered the issue in several
places when testing recent typeshed against an internal repo.
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.

3 participants