8000 Adds `assert_never` helper by sobolevn · Pull Request #3294 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Adds assert_never helper #3294

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 3 commits into from
Aug 9, 2022
Merged

Adds assert_never helper #3294

merged 3 commits into from
Aug 9, 2022

Conversation

sobolevn
Copy link
Contributor
@sobolevn sobolevn commented Oct 5, 2021

Change Summary

I am working on mypy type narrowing bug. And our CI identified that this repo is also affected.
python/mypy#11273 (comment)

What bug am I talking about?
We were narrowing if conditions incorrectly. In your case:

    @staticmethod
    def _coerce_items(items: Union['AbstractSetIntStr', 'MappingIntStrAny']) -> 'MappingIntStrAny':
        if isinstance(items, Mapping):
            pass
        elif isinstance(items, AbstractSet):
            items = dict.fromkeys(items, ...)
        else:
            raise TypeError(f'Unexpected type of exclude value {items.__class__}')
        return items

In else case items were not narrowed to be <nothing>, because Mapping and Set cases are already covered. It was still something like MappingIntStrAny. This is why it was possible to use items.__class__.

But, after new mypy version will be released (and if this fix is merged), this line would raise an error:

pydantic/utils.py:554: error: <nothing> has no attribute "__class__"  [attr-defined]

Because NoReturn does not have __class__ attribute defined.

So, here's my solution on how to fix it: intoduce assert_never helper, docs: https://mypy.readthedocs.io/en/latest/literal_types.html?highlight=assert_never#exhaustive-checks

And type-ignore msg argument.

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

I think, that this is a pretty trivial change to go into changelog / news. If not, feel free to ping me to add it 🙂

@PrettyWood
Copy link
Collaborator

Please update

@github-actions github-actions bot added the awaiting author revision awaiting changes from the PR author label Oct 26, 2021
@samuelcolvin samuelcolvin merged commit 10508f7 into pydantic:master Aug 9, 2022
@samuelcolvin
Copy link
Member

Thanks so much @sobolevn.

@sobolevn
Copy link
Contributor Author
sobolevn commented Aug 9, 2022

Awesome! Thank you for pydantic! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author revision awaiting changes from the PR author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0