[Feature Request] Granular exception messages for synthesized beartype validators
See original GitHub issueHi, I have been studying this library and noticed that the README suggests one prefer conjoining (“and”-ing) conditions over enumerating them (“,”-separating) them.
from dataclasses import dataclass
from typing import Annotated
from beartype import beartype
@dataclass
class Foo:
x: int
y: int
@beartype
def conjoined(
x, y
) -> Annotated[
"Foo",
Is[lambda foo: foo.x + foo.y >= 0] # force separation of lines so only 1 lambda per line
& Is[lambda foo: foo.x + foo.y <= 10],
]:
return Foo(x, y)
conjoined(100, 100)
BeartypeCallHintPepReturnException: @beartyped conjoined() return "Foo(x=100, y=100)" violates type hint typing.Annotated[ForwardRef('Foo'), Is[lambda foo: foo.x + foo.y >= 0] & Is[lambda foo: foo.x + foo.y <= 10]], as "Foo(x=100, y=100)" violates validator Is[lambda foo: foo.x + foo.y >= 0] & Is[lambda foo: foo.x + foo.y <= 10].
However, this doesn’t go in and tell me which condition failed (maybe 1, maybe all).
Enumeration seems to do the trick though:
@beartype
def enumerated(
x, y
) -> Annotated[
"Foo",
Is[lambda foo: foo.x + foo.y >= 0],
Is[lambda foo: foo.x + foo.y <= 10],
]:
return Foo(x, y)
enumerated(100, 100)
BeartypeCallHintPepReturnException: @beartyped enumerated() return "Foo(x=100, y=100)" violates type hint typing.Annotated[ForwardRef('Foo'), Is[lambda foo: foo.x + foo.y >= 0], Is[lambda foo: foo.x + foo.y <= 10]], as "Foo(x=100, y=100)" violates validator Is[lambda foo: foo.x + foo.y <= 10].
To be it seems enumerated conditions could / should be preferred. Could you explain any arguments in favour of conjunction?
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
[Feature Request] Human-readable exception messages for ...
from beartype import beartype class Bar: @beartype @classmethod def foo(cls, s: str) -> int: return int(s) gives uncallable.
Read more >Beartype: Unbearably fast O(1) runtime type-checking in pure ...
Squash bugs by refining type hints with beartype validators. # Import the requisite machinery. >>> from beartype.vale import Is >>> from typing import ......
Read more >beartype/beartype v0.10.0 on GitHub - NewReleases.io
Exception message granularity, including exceptions raised for: Disordered builtin decorators. @beartype now raises instructive exceptions when decorating an ...
Read more >beartype: Versions - Openbase
and | -chained beartype validators now explicitly short-circuit when raising ... Exception message granularity, including exceptions raised for:.
Read more >Spirit Mission Manager Reports - NASA
The project is listening for Spirit with the Deep Space Network and Mars Odyssey orbiter for autonomous recovery communication from the low-power fault...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Quick, someone get @leycec some water — he’s on fire!
This is an awesome feature, think
beartype
0.10 might be an awesome Christmas present!Lol good catch. Without my normal office Mon-Fri coffee, I’m not quite as sharp. I can do
O(n log n)
by myself lol.