Skip to content

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

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks!

@davidhewitt davidhewitt merged commit cc3ac46 into main May 18, 2025
28 of 29 checks passed
@davidhewitt davidhewitt deleted the exc-pos-only branch May 18, 2025 09:16
davidhewitt pushed a commit to pydantic/pydantic that referenced this pull request Oct 20, 2025
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.

BUG -- PydanticKnownError takes no keyword arguments

2 participants