Skip to content

Fix exception types for jsonschema._format - #7990

Merged
AlexWaygood merged 4 commits into
python:masterfrom
sirosen:jsonschema-format-fix-raises
May 30, 2022
Merged

AlexWaygood merged 4 commits into
python:masterfrom
sirosen:jsonschema-format-fix-raises

Conversation

@sirosen

@sirosen sirosen commented May 30, 2022

Copy link
Copy Markdown
Contributor

The annotated type for the raises argument on format checkers was

Exception | tuple[Exception, ...]

when it should read

type[Exception] | tuple[type[Exception], ...]

This is a mistake I introduced in #7950 . So far, there have been two or three issues found with those changes, depending on how you count. I will try to make smaller changes to jsonschema stubs in the future to avoid creating this situation again.

The annotated type for the `raises` argument on format checkers was

    Exception | tuple[Exception, ...]

when it should read

    type[Exception] | tuple[type[Exception], ...]
Comment thread stubs/jsonschema/jsonschema/_format.pyi Outdated
from typing import Any, TypeVar

_F = TypeVar("_F", bound=Callable[..., Any])
_RaisesType = type[Exception] | tuple[type[Exception], ...]

@AlexWaygood AlexWaygood May 30, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You've hit a known mypy bug (python/mypy#11098, python/mypy#12392) in its handling of PEP-604 type aliases. The workaround is to use the old syntax (Union) here instead. The rest of our CI won't complain about it in this instance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, easy enough -- I'll have that done in just a second.

Also, thanks to you and everyone on typeshed for being super responsive! It makes it very easy to contribute. ❤️

@AlexWaygood AlexWaygood May 30, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks to you and everyone on typeshed for being super responsive! It makes it very easy to contribute. ❤️

I'm very glad to hear that! Also, don't beat yourself up too much about the regressions — it's nearly inevitable if you're adding annotations to stubs that currently don't have many type hints at all. One way we could work to reduce these in the future, however, would be to add more open-source projects using types-jsonschema to mypy_primer (https://github.com/hauntsaninja/mypy_primer)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll give that a shot. check-jsonschema is where I'm doing my own work.
And there was a PR from the maintainers of https://github.com/matrix-org/synapse , so perhaps I can integrate that as well. I'll have to see how they have mypy setup, of course.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread stubs/jsonschema/jsonschema/_format.pyi Outdated
@srittau

srittau commented May 30, 2022

Copy link
Copy Markdown
Collaborator

Sorry, my bad, TypeAlias needs to be imported from typing_extensions.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@AlexWaygood
AlexWaygood merged commit 17e7cc7 into python:master May 30, 2022
@sirosen
sirosen deleted the jsonschema-format-fix-raises branch May 30, 2022 16:30
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