Skip to content

Help @beartype help numerary #7

@leycec

Description

@leycec

Greetings and salacious salutations, fellow wayward traveller on the congested superhighway of questionable human experience. Per beartype/beartype#84, I'd love to promote numeric health in downstream code by elevating numerary above lesser and baser alternatives for type-checking numbers with @beartype.

Python numbers are a fathomless well of squalid darkness. I need you to man the lonely walls with me. I have no idea what I do. You do. Together, let us bring light to the dank codebases that think accepting a bool as an int is good API practice.

Consider this hypothetical FAQ entry with your keen hawk eye:

How do I type-check numbers...?

tl;dr: Prefer the third-party numerary package unless you have a compelling reason not to (e.g., you either don't want to add an additional mandatory runtime dependency to your Jenga-like app stack or you do but one of the use cases below already provides a tighter fit).

"Numbers" means many things to many people. Some people think booleans are numbers. Technically, these people are not wrong. Pragmatically, these people are wrong. Don't hire these people.

Stop! Disambiguate and listen. You really want to type-check...

...builtin numeric types covariantly?

You want to type-check int, float, or complex numbers including instances of subclasses of those types? O frabjous day! This is the trivial case, because beartype already does exactly what you want out-of-the-box.

Just annotate with any builtin numeric type as is: e.g.,

@beartype
def fawlty_towers(rare_diseases: int) -> int:
    return rare_diseases ^ 0xDEADBEEF

Be forewarned that any bool is also an int and thus satisfies the above API. If you don't want that, spoiler alert: you don't want that the next question is your codebase's BFFL.

...builtin numeric types invariantly?

Something, something, something. I got exhausted and fell down onto the bed in a dishevelled heap here. This is the toll that type-checking numbers takes on the unprepared body, @posita.

Don't let this happen to you.

it happened

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions