Skip to content

Mark constructor parameters of exceptions as positional-only #1699

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Apr 18, 2025

Change Summary

As these exceptions also inherit from ValueError, providing keyword arguments fails when the super().__new__ call is performed (ValueError only takes positional arguments). Generally, I also tend to enforce pos-only args for exceptions, otherwise the default repr doesn't show the argument:

class MyExc(Exception):
    def __init__(self, message: str) -> None:
        self.message = message

str(MyExc('test'))
# 'test'
str(MyExc(message='test'))
# ''

Also clean up the stub file a bit: avoid duplicating __new__ and __init__ (at runtime, __new__ is the one actually defined but it doesn't play well with mkdocstrings), unify docstrings in classes.

Fixes pydantic/pydantic#10604.

Related issue number

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Copy link

codspeed-hq bot commented Apr 18, 2025

CodSpeed Performance Report

Merging #1699 will not alter performance

Comparing exc-pos-only (f50d17f) with main (3414703)

Summary

✅ 157 untouched benchmarks

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG -- PydanticKnownError takes no keyword arguments
1 participant