question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Granular exception messages for synthesized beartype validators

See original GitHub issue

Hi, 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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dycwcommented, Dec 12, 2021

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!

1reaction
dycwcommented, Nov 20, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found