Skip to content
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

[Feature Request] validate a given object against a type annotation as a function #79

Closed
JakkuSakura opened this issue Dec 31, 2021 · 10 comments

Comments

@JakkuSakura
Copy link

Really love beartype. However, I couldn't find the method for validating a given object against a type annotation. I have to use some tricks. Is this available somewhere?

def check_pep_type_raise_exception(obj, annotation):
    import beartype.roar
    import beartype
    @beartype.beartype
    def check(o) -> annotation:
        return o

    try:
        check(obj)
        reason = None
    except beartype.roar.BeartypeCallHintPepReturnException as e:
        reason = e.args[0].split('return ')[-1]
    if reason:
        raise TypeException(reason)
    return True
@leycec
Copy link
Member

leycec commented Jan 1, 2022

Really love beartype.

Aww! Thanks, Qiu. Right back atcha. 🤗

However, I couldn't find the method for validating a given object against a type annotation.

Finally! Finally someone other than me wants this. Sadly, you are absolutely right. Because you're the first user to express interest in this, beartype currently lacks an explicit API for this. Your implementation is extremely clever, though; I'm insanely impressed with what you've cobbled together out of pure nothingness there. Wowza.

The only real-world issue with your hyper-intelligent approach is runtime efficiency. check_pep_type_raise_exception() dynamically declares one new throw-away closure check() every time it's called. That's absolutely unavoidable, but also absolutely slow. Still...

Your gargantuan intellect stupefies me. You're the first to think of something like that – and you're only a Uni student! 😮

Stumbling into a Bold Future

Let's talk official support. I hope to release beartype 0.10.0 in a few days. Official support for this feature will need to wait until our next beartype 0.11.0 release – which won't be until late January or February. That said, I promise you the official name for this function will be:

  • beartype.is_bearable().

If you'd like to preserve forward compatibility with beartype ≥ 0.11.0, consider something like the following for now:

try:
    from beartype import is_bearable
except ImportError:
    def is_bearable(obj, annotation):
        import beartype.roar
        import beartype
        @beartype.beartype
        def check(o) -> annotation:
            return o
    
        try:
            check(obj)
            reason = None
        except beartype.roar.BeartypeCallHintPepReturnException as e:
            reason = e.args[0].split('return ')[-1]
        if reason:
            raise TypeException(reason)
        return True

When beartype 0.11.0 drops in a month or two, you can then reduce the above to just from beartype import is_bearable. How does that sound, Qiu?

Oh – and thanks so much for suggesting this! I'm delighted to meet a like-minded die-hard equally obsessed with hard-hittin' QA like this. Have a great start to 2022... and beyond.

@JakkuSakura
Copy link
Author

Oh, the API sounds good enough for me. I'm implementing https://github.com/qiujiangkun/typedmodel with beartype as the only dependency, as a replacement of pydantic, but its type-validation is stupid and full of surprise, like treating type annotation standalone and in list differently, when standalone it raises an exception, but in a list, it converts something false negatively. Now with typedmodel, I have a similar experience as I'm working with Rust structs.

I'm in a research group about compiler optimization, code generation, and type inference.

@JakkuSakura JakkuSakura changed the title Feature Request: validate a given object against a type annotation as a function [Feature Request] validate a given object against a type annotation as a function Jan 1, 2022
@leycec
Copy link
Member

leycec commented Jan 2, 2022

pydantic, but its type-validation is stupid and full of surprise, like treating type annotation standalone and in list differently, when standalone it raises an exception, but in a list, it converts something false negatively.

...yikes. Suspicious behaviour like that makes my unibrow waggle suggestively. 🤨

beartype is substantially safer in this regard. Whereas pydantic tries really hard (and occasionally fails) to both validate and transform data, beartype only validates; we never transform anything into something else entirely. Safety first is our motto here.

I've occasionally contemplated adding pydantic-style support for transforms, conversions, and coercions. Then I stumble over issues like those you've submitted and realize I've subconsciously dodged multiple bullets. (Phew!)

Now with typedmodel, I have a similar experience as I'm working with Rust structs.

Yes. Your class Foo(typedmodel.BaseModel): example is endlessly intriguing – especially because the code underlying typedmodel.BaseModel is surprisingly concise and elegant. It's hard to beat 83 lines of code, right?

I mean... that's some extraordinarily efficient code there. I'm now considering adding a new feature strongly inspired by your BaseModel superclass – with attribution to you, of course! – directly to beartype itself. @dataclass.dataclass integration is something we'd all love to see – and BaseModel is an excellent first stab at that.

Kudos yet again there.

I'm in a research group about compiler optimization, code generation, and type inference.

Oh, wow! Congratulations. That sounds utterly fascinating. Drop us all a line when your next thesis and/or paper passes peer review – especially if you leverage beartype throughout your work. I'd love to read that.

@rsokl
Copy link

rsokl commented Jan 7, 2022

+1 for this feature! I will almost certainly have use for this in hydra-zen down the line.

@leycec
Copy link
Member

leycec commented Jan 8, 2022

You got it, our favourite Ryan! @beartype proudly supports all MIT beavers. This is something we've wanted for some time, too. That time is now coming.

The optimistic estimate is that support for this will land with beartype 0.11.0 in late February. Brace yourselves! The ride is gettin' bumpy. 🚁

@JakkuSakura
Copy link
Author

JakkuSakura commented Jan 25, 2022

The previous code has a bug, as in some cases invalid types still pass the test. Here's the fix

try:
    from beartype import is_bearable
except ImportError:
    def is_bearable(obj, annotation):
        import beartype.roar
        import beartype
        import typing
        old_type_checking = typing.TYPE_CHECKING
        typing.TYPE_CHECKING = False
        @beartype.beartype
        def check(o) -> annotation:
            return o

        typing.TYPE_CHECKING = old_type_checking
        try:
            check(obj)
            return True
        except beartype.roar.BeartypeCallHintPepReturnException as e:
            return False

@leycec
Copy link
Member

leycec commented Jan 26, 2022

OMG! Thanks a heap for that, Qiu. We're on the cusp of adding your awesome work to @beartype. So, this is incredibly helpful.

I wonder why typing.TYPE_CHECKING would need temporary monkey-patching, though. That's the conundrum, isn't it? The beartype codebase mostly ignores typing.TYPE_CHECKING. This must be for mypy compatibility, I assume? Do you happen to recall exactly which invalid types were reported as passing? There might be something meaningful we can do about this.

Thanks again, Qiu! You're a living typing legend.

@JakkuSakura
Copy link
Author

JakkuSakura commented Jan 26, 2022

In my project, I check a type against a list of annotations and get the corresponding element. Without this monkey-patching, it won't work sometimes. I'll see how to reproduce it.

I need a way to make sure type checking behave consistently, in case of not __debug__ or not.

if (
    # Optimized (e.g., option "-O" was passed to this interpreter) *OR*...
    not __debug__ or
    # Running under an external static type checker -- in which case there is
    # no benefit to attempting runtime type-checking whatsoever...
    #
    # Note that this test is largely pointless. By definition, static type
    # checkers should *NOT* actually run any code -- merely parse and analyze
    # that code. Ergo, this boolean constant should *ALWAYS* be false from the
    # runtime context under which @beartype is only ever run. Nonetheless, this
    # test is only performed once per process and is thus effectively free.
    TYPE_CHECKING
):

Edit: couldn't reproduce it anymore. Seems it was due to bugs somewhere else, maybe the__debug__ option was false.

@rsokl
Copy link

rsokl commented Jan 26, 2022

Edit: couldn't reproduce it anymore. Seems it was due to bugs somewhere else, maybe the__debug__ option was false.

whew. That makes a lot more sense to me now.

leycec added a commit that referenced this issue Jan 28, 2022
This commit is the first in a commit chain adding a new public generic
type-checking API to @beartype comprising a pair of utility functions
`beartype.abby.is_bearable()` (strictly returning a boolean signifying
whether the passed arbitrary object satisfies the passed type hint or
not) and `beartype.abby.die_if_unbearable()` (raising an exception when
the passed arbitrary object violates the passed type hint), en-route to
resolving feature request #79 kindly submitted by (*...wait for it*)
typing Kung Fu master @qiujiangkun. Specifically, this commits adds a
thoroughly untested and undocumented first-draft implementation of the
`beartype.abby.is_bearable()` tester inspired by (*...wait for it*)
@qiujiangkun's impeccable contributions. (*Surly absurdists surely abdicate!*)
leycec added a commit that referenced this issue Jan 29, 2022
This commit is the next in a commit chain adding a new public generic
type-checking API to @beartype comprising a pair of utility functions
`beartype.abby.is_bearable()` (strictly returning a boolean signifying
whether the passed arbitrary object satisfies the passed type hint or
not) and `beartype.abby.die_if_unbearable()` (raising an exception when
the passed arbitrary object violates the passed type hint), en-route to
resolving feature request #79 kindly submitted by (*...wait for it*)
typing Kung Fu master @qiujiangkun. Specifically, this commits offloads
the guts of the `beartype.abby.__init__` submodule to a new private
`beartype.abby._abbytest` submodule for maintainability. (*Provocatively evocative evocation!*)
leycec added a commit that referenced this issue Feb 1, 2022
This commit is the next in a commit chain adding a new public generic
type-checking API to @beartype comprising a pair of utility functions
`beartype.abby.is_bearable()` (strictly returning a boolean signifying
whether the passed arbitrary object satisfies the passed type hint or
not) and `beartype.abby.die_if_unbearable()` (raising an exception when
the passed arbitrary object violates the passed type hint), en-route to
resolving feature request #79 kindly submitted by (*...wait for it*)
typing Kung Fu master @qiujiangkun. Specifically, this commits
voluminously documents and comments the critical
`beartype.abby.is_bearable()` tester. (*Crunchy munching!*)
leycec added a commit that referenced this issue Feb 3, 2022
This commit is the next in a commit chain adding a new public generic
type-checking API to @beartype comprising a pair of utility functions
`beartype.abby.is_bearable()` (strictly returning a boolean signifying
whether the passed arbitrary object satisfies the passed type hint or
not) and `beartype.abby.die_if_unbearable()` (raising an exception when
the passed arbitrary object violates the passed type hint), en-route to
resolving feature request #79 kindly submitted by (*...wait for it*)
typing Kung Fu master @qiujiangkun. Specifically, this commit
generalizes our new `beartype.abby.is_bearable()` tester to additionally
accept an optional keyword-only ``conf`` parameter configuring
type-checking for the current call. (*Massive missive!*)
leycec added a commit that referenced this issue Feb 4, 2022
This commit is the next in a commit chain adding a new public generic
type-checking API to @beartype comprising a pair of utility functions
`beartype.abby.is_bearable()` (strictly returning a boolean signifying
whether the passed arbitrary object satisfies the passed type hint or
not) and `beartype.abby.die_if_unbearable()` (raising an exception when
the passed arbitrary object violates the passed type hint), en-route to
resolving feature request #79 kindly submitted by (*...wait for it*)
typing Kung Fu master @qiujiangkun. Specifically, this commit declares a
new `beartype.roar.BeartypeCallHintTypeAbbyException` exception class to
be subsequently raised by our new `beartype.abby.die_if_unbearable()`
validator which admittedly currently does nothing. Unrelatedly, this
commit also resolves mypy complaints with respect to recently merged
pull request #88. You be quiet, mypy. You be quiet right now, you hear?
(*Fastidious pigeon!*)
leycec added a commit that referenced this issue Feb 7, 2022
This commit is the last in a commit chain adding a new public generic
type-checking API to @beartype comprising a pair of utility functions
`beartype.abby.is_bearable()` (strictly returning a boolean signifying
whether the passed arbitrary object satisfies the passed type hint or
not) and `beartype.abby.die_if_unbearable()` (raising an exception when
the passed arbitrary object violates the passed type hint), resolving
feature request #79 kindly submitted by (*...wait for it*) typing Kung
Fu master @qiujiangkun. Specifically, this commit finalizes the
implementations, documentation, and unit tests of both the
`beartype.abby.is_bearable()` tester and companion
`beartype.abby.die_if_unbearable()` validator. Unrelatedly, this commit
also deprecates a trio of poorly named exception classes as follows:

* `beartype.roar.BeartypeCallHintPepException`, deprecated by
  `beartype.roar.BeartypeCallHintViolation`.
* `beartype.roar.BeartypeCallHintPepParamException`, deprecated by
  `beartype.roar.BeartypeCallHintParamViolation`.
* `beartype.roar.BeartypeCallHintPepReturnException`, deprecated by
  `beartype.roar.BeartypeCallHintReturnViolation`.

(*Regurgitating gurgles agitate an unrated titration!*)
@leycec
Copy link
Member

leycec commented Feb 7, 2022

It is done. Weep with satisfaction as beartype now type-checks everything for you in constant-time on-demand. Check this now, beartype!

>>> from beartype.abby import is_bearable
>>> is_bearable(['Things', 'fall', 'apart;'], list[str])
True
>>> is_bearable(['the', 'centre', 'cannot', 'hold;'], list[int])
False

>>> from beartype.abby import die_if_unbearable
>>> die_if_unbearable(['And', 'what', 'rough', 'beast,'], list[str])
>>> die_if_unbearable(['its', 'hour', 'come', 'round'], list[int])
beartype.roar.BeartypeAbbyHintViolation: Object ['its', 'hour', 'come',
'round'] violates type hint list[int], as list index 0 item 'its' not
instance of int.

😮

Caveat emptor: the current implementation is painfully unoptimized, but will be optimized shortly. Since a slow API that exists >>>> a fast API that doesn't exist, we hope everyone will bear ...heh with this temporary slowdown. This API should more than fast enough for general-purpose use, but cracks will show if you throw it at a tight inner loop.

@leycec leycec closed this as completed Feb 7, 2022
leycec added a commit that referenced this issue Feb 9, 2022
This release titillates with scintillating support for **[PEP 557 --
Data Classes][PEP 557]**, **[PEP 570 -- Python Positional-Only
Parameters][PEP 570]**, and **[PEP 604 -- Allow writing union types as
X | Y][PEP 604]**.

This release resolves a bone-crushing **30 issues** (mostly shameless
dupes of one another, admittedly) and merges **3 pull requests.**
World-girdling changes include:

## Compatibility Added

* **[PEP 557 -- Data Classes][PEP 557].** `@beartype` now supports
  **dataclasses** (i.e., types decorated by the standard
  `@dataclasses.dataclass` decorator), resolving issue #56 kindly
  submitted by @JulesGM (Jules Gagnon-Marchand) the Big Brain NLP
  researcher. Specifically, `@beartype` now transparently type-checks:
  * **Dataclass-specific initialization-only instance variable type
    hints** (i.e., `dataclasses.InitVar[...]`).
  * The implicit `__init__()` method generated by `@dataclass` for
    dataclasses through a clever one-liner employed by @antonagestam
    (Anton Agestam) the ageless Swede that I stan for.
* **[PEP 570 -- Python Positional-Only Parameters][PEP 570].**
  `@beartype` now supports positional-only arguments and no one cares.
  Given the triviality, the rear view mirror of regret suggests we kinda
  should've implemented this sooner. Better late than never, best
  @beartype friends for life (BBFFL).
* **[PEP 604 -- Allow writing union types as X | Y][PEP 604].**
  `@beartype` now supports new-style set unions (e.g., `int | float`),
  resolving issue #71 kindly submitted by pro typing aficionado Derek
  Wan (@dycw). Thanks to Derek for the helpful heads up that @beartype
  was headed straight for typing disaster under Python ≥ 3.10. Since we
  dodged another bullet there, this must mean we have now activated
  bullet time. Goooooo, slomo!

## Compatibility Improved

* **[PEP 484 -- Type Hints][PEP 484],** including:
  * **`typing.{Binary,Text,}IO[...]` deep type-checking.** `@beartype`
    now deeply type-checks subscripted `typing.{Binary,Text,}IO[...]`
    type hints, resolving issue #75 kindly submitted by Niklas "If I had
    a nickel for every lass..." Rosenstein. Notably:
    * Since the `typing.BinaryIO` protocol and its `typing.IO`
      superclass share the exact same API, the `typing.BinaryIO`
      protocol is lamentably useless for *all* practical purposes. This
      protocol *cannot* be leveraged to detect binary file handles. Can
      binary file handles be detected at runtime then? Yes, we can! A
      binary file handle is any object satisfying the `typing.IO`
      protocol but *not* the `typing.TextIO` protocol. To implement this
      distinction, `@beartype` necessarily invented a novel form of
      type-checking and a new variant of type elision: **anti-structural
      subtyping.** Whereas structural subtyping checks that one class
      matches the API of another class (referred to as a "protocol"),
      anti-structural subtyping checks that one class does *not* match
      the API of another class (referred to as an "anti-protocol").
      `@beartype` public exposes this functionality via the new
      `beartype.vale.IsInstance[...]` validator, enabling *anyone* to
      trivially perform anti-structural subtyping. In this case,
      `@beartype` internally reduces all useless `typing.BinaryIO` type
      hints to substantially more useful `typing.Annotated[typing.IO,
      ~beartype.vale.IsInstance[typing.TextIO]]` type hints.
* **Unsubscripted NumPy type hints.** `@beartype` now supports **untyped
  NumPy array type hints** (i.e., the unsubscripted
  `numpy.typing.NDArray` and subscripted
  `numpy.typing.NDArray[typing.Any]` type hints), resolving issue #69
  kindly submitted by @Jasha10, the stylish boy wonder dual-wielding the
  double thumbs-up and coke-bottle glasses that signify elementary
  genius. Specifically, this commit now detects and reduces these hints
  to the equivalent `numpy.ndarray` type.
* **Mypy ≥ 0.920.** `@beartype` now squelches ignorable mypy complaints
  first introduced by mypy 0.920, including:
  * **Explicit reexport errors.** `beartype` now squelches implicit
    reexport complaints from mypy with respect to public attributes
    published by the `beartype.cave` subpackage, resolving issue #57
    kindly reopened by Göteborg melodic death metal protégé and
    brightest academic luminary @antonagestam. This subpackage is now
    compatible with both the `--no-implicit-reexport` mypy CLI option
    and equivalent `no_implicit_reexport = True` configuration setting
    in `.mypy.ini`.
  * **Version-dependent errors.** Previously, mypy permitted imports
    against standard library modules introduced in newer CPython
    versions to be squelched with the usual ``"# type:
    ignore[attr-defined]"`` pragma. Since mypy now ignores these
    pragmas, `@beartype` now silences its complaints through...
    *unconventional* means. A bear do wut a bear gotta do.

## Features Added

* **Compatibility API.** `beartype` now publishes a new
  `beartype.typing` API as a `typing` compatibility layer improving
  forward compatibility with future Python releases, resolving issue #81
  kindly submitted by the honorable @qiujiangkun (Qiu Jiangkun).
  Consider resolving [PEP 585][PEP 585] deprecations by importing from
  our new `beartype.typing` API rather than the standard `typing` API. A
  battery of new unit tests ensure conformance:
  * Between `beartype.typing` and `typing` across all Python versions.
  * With mypy when importing from `beartype.typing`.
* **Configuration API** (i.e., public attributes of the `beartype`
  package enabling end users to configure the `@beartype` decorator,
  including configuring alternative type-checking strategies *other*
  than constant-time runtime type-checking). Specifically, `beartype`
  now publishes:
  * `beartype.BeartypeStrategy`, an enumeration of all type-checking
    strategies to *eventually* be fully supported by future beartype
    releases – including:
    * `BeartypeStrategy.O0`, disabling type-checking for a callable by
      reducing `@beartype` to the identity decorator for that callable.
      Although currently useless, this strategy will usefully allow end
      users to selectively prevent callables from being type-checked by
      our as-yet-unimplemented import hook. When implemented, that hook
      will type-check *all* callables in a given package by default.
      Some means is needed to prevent that from happening for select
      callables. This is that means.
    * `BeartypeStrategy.O1`, our default `O(1)` constant-time strategy
      type-checking a single randomly selected item of a container that
      you currently enjoy. Since this is the default, this strategy need
      *not* be explicitly configured. Of course, you're going to do that
      anyway, aren't you? `</sigh>`
    * `BeartypeStrategy.Ologn`, a new `O(lgn)` logarithmic strategy
      type-checking a randomly selected number of items `j` of a
      container `obj` such that `j = len(obj)`. This strategy is
      **currently unimplemented** (but will be implemented by a future
      beartype release).
    * `BeartypeStrategy.On`, a new `O(n)` linear strategy
      deterministically type-checking *all* items of a container. This
      strategy is **currently unimplemented** (but will be implemented
      by a future beartype release).
  * `beartype.BeartypeConf`, a simple dataclass encapsulating all flags,
    options, settings, and other metadata configuring the current
    decoration of the decorated callable or class. For efficiency, this
    dataclass internally self-caches itself (i.e.,
    `BeartypeConf(*args, **kwargs) is BeartypeConf(*args, **kwargs)`).
    The `__init__()` method of this dataclass currently accepts these
    optional parameters:
    * An `is_debug` boolean instance variable. When enabled, `@beartype`
      emits debugging information for the decorated callable – including
      the code for the wrapper function dynamically generated by
      `@beartype` that type-checks that callable.
    * A `strategy` instance variable whose value must be a
      `BeartypeStrategy` enumeration member. This is how you notify
      `@beartype` of which strategy to apply to each callable.
  * **Wrapper function debuggability.** Enabling the `is_debug`
    parameter to the `BeartypeConf.__init__` method significantly
    improves the debuggability of type-checking wrapper functions
    generated by `@beartype`. This configuration option is entirely
    thanks to @posita the positive Numenorean, who pined longingly for
    debuggable wrapper functions and now receives proportionately.
    Praise be to @posita! He makes bears better. Specifically, enabling
    this option enables developer-friendly logic like:
    * Pretty-printing to stdout (standard output) the definitions of
      those functions, including line number prefixes for readability.
    * Enabling those functions to be debugged. Thanks to a phenomenal
      pull request by the dynamic dual threat that is @posita **+**
      @TeamSpen210, `@beartype` now conditionally caches the bodies of
      type-checking wrapper functions with the standard (albeit poorly
      documented) `linecache` module. Thanks so much! Bear Clan 2022!!!
    * Suffixing the declarations of `@beartype`-specific hidden private
      "special" parameters passed to those functions with comments
      embedding their human-readable representations. Safely generating
      these comments consumes non-trivial wall clock at decoration time
      and is thus conditionally enabled for external callers requesting
      `@beartype` debugging. For example, note the `"# is"`-prefixed
      comments in the following signature of a `@beartype`-generated
      wrapper function for an asynchronous callable with signature
      `async def control_the_car(said_the: Union[str, int],
      biggest_greenest_bat: Union[str, float]) -> Union[str, float]:`

      ``` python
      (line 0001) async def control_the_car(
      (line 0002)     *args,
      (line 0003)     __beartype_func=__beartype_func, # is <function test_decor_async_coroutine.<locals>.control_the_car at 0x7>
      (line 0004)     __beartype_raise_exception=__beartype_raise_exception, # is <function raise_pep_call_exception at 0x7fa13d>
      (line 0005)     __beartype_object_140328307018000=__beartype_object_140328307018000, # is (<class 'int'>, <class 'str'>)
      (line 0006)     __beartype_object_140328306652816=__beartype_object_140328306652816, # is (<class 'float'>, <class 'str'>)
      (line 0007)     **kwargs
      (line 0008) ):
      ```
* **Decorator modality.** `@beartype` now supports two orthogonal modes
  of operation:
  * **Decoration mode** (i.e., the standard mode where `@beartype`
    directly decorates a callable *without* being passed parameters). In
    this mode, `@beartype` reverts to the default configuration of
    constant-time runtime type-checking and *no* debugging behaviour.
  * **Configuration mode** (i.e., the new mode where `@beartype` is
    called as a function passed a `BeartypeConf` object via the
    keyword-only `conf` parameter). In this mode, `@beartype`
    efficiently creates, caches, and returns a memoized decorator
    encapsulating the passed configuration: e.g.,

    ``` python
    from beartype import beartype, BeartypeConf, BeartypeStrategy

    @beartype(conf=BeartypeConf(strategy=BeartypeStrategy.On))
    def muh_func(list_checked_in_linear_time: list[int]) -> int:
        return len(list_checked_in_linear_time)
    ```
  * Specifically, this commit extricates our core
    `@beartype` decorator into a new private `beartype._decor._core`
    submodule in preparation for subsequently memoizing closures
    encapsulating that decorator returned by invocations of the form
    `@beartype.beartype(conf=BeartypeConf(...))`
* **Declarative instance validator.** `beartype` now publishes a new
  `beartype.vale.IsInstance[...]` validator enforcing instancing of one
  or more classes, generalizing **isinstanceable type hints** (i.e.,
  normal pure-Python or C-based classes that can be passed as the second
  parameter to the ``isinstance()`` builtin). Unlike standard
  isinstanceable type hints, `beartype.vale.IsInstance[...]` supports
  various set theoretic operators. Critically, this includes negation.
  Instance validators prefixed by the negation operator `~` match all
  objects that are *not* instances of the classes subscripting those
  validators. Wait. Wait just a hot minute there. Doesn't a
  typing.Annotated_ type hint necessarily match instances of the class
  subscripting that type hint? Yup. This means type hints of the form
  `typing.Annotated[{superclass}, ~IsInstance[{subclass}]` match all
  instances of a superclass that are *not* also instances of a subclass.
  And... pretty sure we just invented type hint arithmetic right there.
  That sounded intellectual and thus boring. Yet, the disturbing fact that
  Python booleans are integers <sup>yup</sup> while Python strings are
  infinitely recursive sequences of strings <sup>yup</sup> means that
  type hint arithmetic can save your codebase from Guido's younger self.
  Consider this instance validator matching only non-boolean integers,
  which *cannot* be expressed with any isinstanceable type hint (e.g.,
  ``int``) or other combination of standard off-the-shelf type hints
  (e.g., unions): `Annotated[int, ~IsInstance[bool]]`. ← *bruh*
* **Functional API.** `beartype` now publishes a new public
  `beartype.abby` subpackage enabling users to type-check *anything*
  *anytime* against *any* PEP-compliant type hints, resolving feature
  request #79 kindly submitted by (*...wait for it*) typing Kung Fu
  master @qiujiangkun (Qiu Jiangkun). This subpackage is largely thanks
  to @qiujiangkuni, whose impeccable code snippets drive our initial
  implementation. This subpackage provides these utility functions:
  * `beartype.abby.is_bearable()`, strictly returning a boolean
    signifying whether the passed arbitrary object satisfies the passed
    type hint or not (e.g., `is_bearable(['the', 'centre', 'cannot',
    'hold;'], list[int]) is False`).
  * `beartype.abby.die_if_unbearable()`, raising the new
    `beartype.roar.BeartypeAbbyHintViolation` exception when the passed
    arbitrary object violates the passed type hint.

## Features Improved

* **Exception message granularity,** including exceptions raised for:
  * **Disordered builtin decorators.** `@beartype` now raises
    instructive exceptions when decorating an uncallable descriptor
    created by a builtin decorator (i.e., `@property`, `@classmethod`,
    `@staticmethod`) due to the caller incorrectly ordering `@beartype`
    above rather than below that decorator, resolving issue #80 kindly
    submitted by typing academician @qiujiangkun (Qiu Jiangkun).
    Specifically, `@beartype` now raises human-readable exceptions
    suffixed by examples instructing callers to reverse decoration
    ordering.
  * **Beartype validators.** `@beartype` now appends a detailed
    pretty-printed diagnosis of how any object either satisfies or fails
    to satisfy any beartype validator to exception messages raised by
    high-level validators synthesized from lower-level validators (e.g.,
    via overloaded set theoretic operators like `|`, `&`, and `~`),
    resolving issue #72 kindly submitted by the unwreckable type-hinting
    guru Derek Wan (@dycw). This diagnostic trivializes validation
    failures in non-trivial use cases involving multiple nested
    conjunctions, disjunctions, and/or negations.

## Features Optimized

* **`@beartype` call-time performance.** `@beartype` now generates
  faster type-checking wrapper functions with a vast and undocumented
  arsenal of absolutely "legal" weaponry, including:
  * **`typing.{Generic,Protocol}` deduplication.** `@beartype` now
    microoptimizes away redundant `isinstance()` checks in wrapper
    functions checking `@beartype`-decorated callables annotated by
    **PEP 484-compliant subgenerics or PEP 585-compliant subprotocols**
    (i.e., user-defined classes subclassing user-defined classes
    subclassing `typing.{Generic, Protocol}`), resolving issue #76
    kindly submitted by @posita the positive numerics QA guru and
    restoring the third-party `numerary` package to its glory. Our
    generics workflow has been refactored from the ground-up to stop
    behaving insane. `@beartype` now performs an inner breadth-first
    search (BFS) across generic pseudo-superclasses in its existing
    outer BFS that generates type-checking code. When you're nesting a
    BFS-in-a-BFS, your code went full-send. There's no going back from
    that.
 * **Worst-case nested data structures.** `@beartype` now resolves a
   performance regression in type-checking wrapper functions passed
   worst-case nested data structures violating PEP-compliant type hints,
   resolving issue #91 kindly submitted by Cuban type-checking
   revolutionary @mvaled (Manuel Vázquez Acosta). Specifically, this
   commit safeguards our low-level `represent_object()` function
   stringifying objects embedded in exception messages describing
   type-checking violations against worst-case behaviour. A new unit
   test shieldwalls against further performance regressions. All our
   gratitude to @mvaled for unveiling the darkness in the bear's heart.
* **`@beartype` decoration-time performance.** The `@beartype` decorator
  has been restored to its prior speed, resolving performance
  regressions present throughout our [0.8.0, 0.10.0) release cycles.
  Significant decoration-time optimizations include:
  * **Code objects.** `@beartype` now directly accesses the code object
    underlying the possibly unwrapped callable being decorated via a
    temporary cache rather than indirectly accessing that code object by
    repeatedly (and expensively) unwrapping that callable, dramatically
    optimizing low-level utility functions operating on code objects.
  * **Exception messages.** `@beartype` now defers calling expensive
    exception handling-specific functions until an exception is raised,
    dramatically restoring our decoration-time performance to the
    pre-0.8.0 era – which isn't that great, honestly. But we'll take
    anything. Substantial optimizations remain, but we are dog-tired.
    Moreover, DQXIS:EofaEA (...that's some catchy name right there)
    ain't gonna play itself – *OR IS IT!?!* Cue creepy AI.
  * **Fixed lists.** `@beartype` now internally lelaxes inapplicable
    safety measures previously imposed by our internal `FixedList`
    container type. Notably, this type previously detected erroneous
    attempts to extend the length of a fixed list by subversively
    assigning a slice of that fixed list to a container whose length
    differs from that of that slice. While advisable in theory,
    `@beartype` *never* actually sliced any fixed list -- let alone used
    such a slice as the left-hand side (LHS) of an assignment. Disabling
    this detection measurably improves the efficiency of fixed lists
    across the codebase -- which is, after all, the entire raison d'etre
    for fixed lists in the first place. `</shaking_my_head>`
  * **Parameter introspection.** `@beartype` now introspects callable
    signatures using a homegrown lightweight parameter parsing API.
    `@beartype` previously introspected signatures using the standard
    heavyweight `inspect` module, which proved... *inadvisable.*
    All references to that module have been removed from timing-critical
    code paths. All remaining references reside only in timing-agnostic
    code paths (e.g., raising human-readable exceptions for beartype
    validators defined as anonymous lambda functions).
* **`@beartype` importation-time performance.** The `beartype` package
  now avoids unconditionally importing optional first- and third-party
  subpackages, improving the efficiency of the initial ``from beartype
  import beartype`` statement in particular. `beartype` now
  intentionally defers these imports from global module scope to the
  local callable scope that requires them. A new functional test
  guarantees this to be the case.

## Features Deprecated

* **Badly named exception classes,** to be removed in `beartype` 0.1.0.
  This includes:
  * `beartype.roar.BeartypeCallHintPepException`, deprecated by
    `beartype.roar.BeartypeCallHintViolation`.
  * `beartype.roar.BeartypeCallHintPepParamException`, deprecated by
    `beartype.roar.BeartypeCallHintParamViolation`.
  * `beartype.roar.BeartypeCallHintPepReturnException`, deprecated by
    `beartype.roar.BeartypeCallHintReturnViolation`.

## Documentation Revised

* The *Frequently Asked Questions (FAQ)* section of our front-facing
  `README.rst` documentation now sports a medley of new entries,
  including instructions on:
  * **Boto3 integration,** enabling end users to type-check runtime
    types dynamically fabricated by Boto3 (i.e., the official Amazon Web
    Services (AWS) Software Development Kit (SDK) for Python), resolving
    issue #68 kindly submitted by Paul Hutchings (@paulhutchings) – the
    supremely skilled sloth rockin' big shades and ever bigger
    enthusiasm for well-typed Python web apps. Relatedly, the @beartype
    organization now officially hosts [`bearboto3`, Boto3 @beartype
    bindings by (*wait for it*) @paulhutchings](https:
    //github.com/beartype/bearboto3).
  * **Mock type type-checking,** resolving issue #92 kindly submitted by
    @Masoudas (Masoud Aghamohamadian-Sharbaf – wish I had an awesome
    name like that). Gratuitous shoutouts to @TeamSpen210 for the quick
    save with a ludicrous two-liner solving everything.

  [PEP 484]: https://www.python.org/dev/peps/pep-0484
  [PEP 557]: https://www.python.org/dev/peps/pep-0557
  [PEP 570]: https://www.python.org/dev/peps/pep-0570
  [PEP 585]: https://www.python.org/dev/peps/pep-0585
  [PEP 604]: https://www.python.org/dev/peps/pep-0604

The hype train is now boarding. All aboooooooard! (*Classless masterless masterclass!*)
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

No branches or pull requests

3 participants