Skip to main content

Based static typing for Python

Project description

Ever tried to use pythons type system and thought to yourself “This doesn’t seem based”.

Well fret no longer as basedmypy got you covered!

Baseline

Basedmypy has baseline, baseline is based! It allows you to adopt new strictness or features without the burden of fixing up every usage, just save all current errors to the baseline file and deal with them later.

def foo(a):
    print(a)
> mypy test.py
error: missing typehints !!!!!
Epic fail bro!

> mypy --write-baseline test.py
test.py:1:1: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
Baseline successfully written to .mypy/baseline.json

> mypy test.py
Success: no issues found in 1 source file

Then on subsequent runs the existing errors will be filtered out.

def foo(a):
    print(a)

def bar(b: str, c: int) -> bool:
    return b + c
> mypy test.py
test.py:4:5: error: Returning Any from function declared to return "bool"  [no-any-return]
test.py:4:16: error: Unsupported operand types for + ("str" and "int")  [operator]
Found 2 errors in 1 file (checked 1 source file)

Intersection Types

Using the & operator or basedtyping.Intersection you can denote intersection types.

class Growable(ABC, Generic[T]):
    @abstractmethod
    def add(self, item: T): ...

class Resettable(ABC):
    @abstractmethod
    def reset(self): ...

def f(x: Resettable & Growable[str]):
    x.reset()
    x.add("first")

Type Joins

Mypy joins types like so:

a: int
b: str
reveal_type(a if bool() else b)  # Revealed type is "builtins.object"

Basedmypy joins types into unions instead:

a: int
b: str
reveal_type(a if bool() else b)  # Revealed type is "int | str"

Bare Literals

Literal is so cumbersome! just use a bare literal instead.

class Color(Enum):
    RED = auto()

a: 1 | 2
b: True | Color.RED

Default Return Type

With the default_return option, the default return type of functions becomes None instead of Any.

def f(name: str):
    print(f"Hello, {name}!")

reveal_type(f)  # (str) -> None

Nested TypeVars

With nested TypeVars you are able to have functions with polymorphic generic parameters.

E = TypeVar("E")
I = TypeVar("I", bound=Iterable[E])

def foo(i: I, e: E) -> I:
    assert e not in i
    return i

reveal_type(foo(["based"], "mypy"))  # N: Revealed type is "list[str]"
reveal_type(foo({1, 2}, 3))  # N: Revealed type is "set[int]"

Overload Implementation Inference

Specifying types in overload implementations is completely redundant! basedmypy will infer them.

@overload
def f(a: int) -> str: ...

@overload
def f(a: str) -> int: ...

def f(a):
    reveal_type(a)  # int | str
    return None  # error: expected str | int

class A:
    @property
    def foo(self) -> int: ...
    @foo.setter
    def foo(self, value): ...  # no need for annotations

Infer Function Parameters

Infer the type of a function parameter from it’s default value.

def f(a=1, b=True):
    reveal_type((a, b))  # (int, bool)

Better Types in Messages

T = TypeVar("T", bound=int)

def f(a: T, b: list[str | 1 | 2]) -> Never:
    reveal_type((a, b))

reveal_type(f)

Mypy shows:

Revealed type is "Tuple[T`-1, Union[builtins.str, Literal[1], Literal[2]]]"
Revealed type is "def [T <: builtins.int] (a: T`-1, b: Union[builtins.str, Literal[1], Literal[2]]) -> <nothing>"

Basedmypy shows:

Revealed type is "(T@f, str | 1 | 2)"
Revealed type is "def [T: int] (a: T, b: str | 1 | 2) -> Never"

Ignore Unused Type Ignores

In code that is targeting multiple versions of python or multiple platforms it is difficult to work with type: ignore comments and use the warn_unused_ignore option.

The unused-ignore error code can be used for this situation.

if sys.platform != "linux":
  foo()  # type: ignore[misc, unused-ignore]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

basedmypy-1.8.0rc1.tar.gz (2.8 MB view hashes)

Uploaded Source

Built Distributions

basedmypy-1.8.0rc1-py3-none-any.whl (2.4 MB view hashes)

Uploaded Python 3

basedmypy-1.8.0rc1-cp311-cp311-win_amd64.whl (9.1 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

basedmypy-1.8.0rc1-cp311-cp311-musllinux_1_1_x86_64.whl (20.4 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

basedmypy-1.8.0rc1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

basedmypy-1.8.0rc1-cp311-cp311-macosx_11_0_arm64.whl (10.6 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

basedmypy-1.8.0rc1-cp311-cp311-macosx_10_9_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

basedmypy-1.8.0rc1-cp311-cp311-macosx_10_9_universal2.whl (19.8 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

basedmypy-1.8.0rc1-cp310-cp310-win_amd64.whl (9.1 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

basedmypy-1.8.0rc1-cp310-cp310-musllinux_1_1_x86_64.whl (20.6 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

basedmypy-1.8.0rc1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

basedmypy-1.8.0rc1-cp310-cp310-macosx_11_0_arm64.whl (10.7 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

basedmypy-1.8.0rc1-cp310-cp310-macosx_10_9_x86_64.whl (11.7 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

basedmypy-1.8.0rc1-cp310-cp310-macosx_10_9_universal2.whl (20.0 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

basedmypy-1.8.0rc1-cp39-cp39-win_amd64.whl (9.1 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

basedmypy-1.8.0rc1-cp39-cp39-musllinux_1_1_x86_64.whl (20.6 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

basedmypy-1.8.0rc1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

basedmypy-1.8.0rc1-cp39-cp39-macosx_11_0_arm64.whl (10.7 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

basedmypy-1.8.0rc1-cp39-cp39-macosx_10_9_x86_64.whl (11.7 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

basedmypy-1.8.0rc1-cp39-cp39-macosx_10_9_universal2.whl (20.0 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

basedmypy-1.8.0rc1-cp38-cp38-win_amd64.whl (9.1 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

basedmypy-1.8.0rc1-cp38-cp38-musllinux_1_1_x86_64.whl (20.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

basedmypy-1.8.0rc1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

basedmypy-1.8.0rc1-cp38-cp38-macosx_11_0_arm64.whl (10.7 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

basedmypy-1.8.0rc1-cp38-cp38-macosx_10_9_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

basedmypy-1.8.0rc1-cp38-cp38-macosx_10_9_universal2.whl (19.9 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

basedmypy-1.8.0rc1-cp37-cp37m-win_amd64.whl (8.8 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

basedmypy-1.8.0rc1-cp37-cp37m-musllinux_1_1_x86_64.whl (18.0 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

basedmypy-1.8.0rc1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

basedmypy-1.8.0rc1-cp37-cp37m-macosx_10_9_x86_64.whl (11.3 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page