-
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Docos] New FAQ entry on "Why are type-checking violation messages so unreadable, long, bad, and awful?" #311
Comments
Hi @leycec I think I may put a last nail in this coffin, by proposing a last modification to the hint_overrides option. First, two tiny problems
Now, one big final solutionWe just added an option
It could as well say:
This could be as simple as using (name, type) tuples in the BeartypeHintOverrides values: The use case (warning, it's very shiny and could hurt your eyes)from typing import NewType, TYPE_CHECKING
from numpy.typing import ArrayLike as npt_ArrayLike
from beartype import (
beartype,
BeartypeConf,
BeartypeHintOverrides,
)
if TYPE_CHECKING:
# mypy is running. ArrayLike = ArrayLike
ArrayLike = npt_ArrayLike
else:
# Define a dummy type for now
# We will explicitely tell beartype how to handle this dummy type
ArrayLike = NewType("ArrayLike", None)
typecheck = beartype(
conf=BeartypeConf(
hint_overrides=BeartypeHintOverrides(
{ArrayLike: ("ArrayLike", npt_ArrayLike)}
),
)
) Now, mypy is still happy with its long ArrayLike Union, beartype is told exactly what to do with The questionDo you want me to implement this hint_overrides tuple configuration? I know exactly what to do. |
Note to future self: "See this comment at #216 for a fully working example Note to past self: "Invest in everything by this guy named Sam Altman." |
Quebec data science superstart @felixchenier just discovered a hitherto unacknowledged workaround for generating human-readable type-checking violation messages from otherwise unreadable type hints like NumPy's own
numpy.typing.ArrayLike
.ArrayLike
is a really non-trivial union of, like, a billion different low-level private types and type hints scattered throughout the NumPy codebase. Type-checking violation messages involvingArrayLike
necessarily devolve into a barren scrum of terminal gore and eye-gouging ASCII.Take it away, Felix-bro!
4D Chess Moves R Us
OMG. That solution is insanely clever. I'd actually never thought of abusing
typing.NewType
to generate human-readable violation messages from otherwise unreadable type hints likenumpy.typing.ArrayLike
. Neither has anyone else, frankly. You're definitely the first to discover an actual use case fortyping.NewType
. In fact, I'd writtentyping.NewType
off as utterly useless for everything.4D chess move right there. Seriously. I'm dead serious here. Let's publicly document this for everyone with a new official FAQ entry appropriately entitled:
The text was updated successfully, but these errors were encountered: