Package: basedmypy
Basedmypy is a type checker that is built on top of the work done by themypy project. It adds based functionality and breaks compatibility with
the cringe parts of pep 484.Based featuresBaselineBasedmypy 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.Consider the following:deffoo(a):print(a)> mypy demo.py
demo.py:1: error: missing typehints
Failed: errors found in source file


> mypy --write-baseline demo.py
demo.py:1: error: missing typehints
Baseline successfully written to .mypy/baseline.json

> mypy demo.py
Success: no issues found in 1 source fileThen on subsequent runs the existing errors will be filtered out:deffoo(a):print(a)defbar(b:str,c:int)->bool:returnb+c> mypy demo.py
demo.py:4:5: error: Returning Any from function declared to return "bool"  [no-any-return]
demo.py:4:16: error: Unsupported operand types for + ("str" and "int")  [operator]
Found 2 errors in 1 file (checked 1 source file)Intersection TypesUsing the&operator orbasedtyping.Intersectionyou can denote intersection types:classGrowable(ABC,Generic[T]):@abstractmethoddefadd(self,item:T):...classResettable(ABC):@abstractmethoddefreset(self):...deff(x:Resettable&Growable[str]):x.reset()x.add("first")Type JoinsMypy joins types to their common base type:a:intb:strreveal_type(aifbool()elseb)# Revealed type is "builtins.object"Basedmypy joins types into unions instead:a:intb:strreveal_type(aifbool()elseb)# Revealed type is "int | str"Bare LiteralsLiteralis so cumbersome! just use a bare literal instead:classColor(Enum):RED=auto()a:1|2b:True|Color.REDDefault Return TypeThe default return type of functions isNoneinstead ofAny:
(configurable with thedefault_returnoption.)deff(name:str):print(f"Hello,{name}!")reveal_type(f)# (str) -> NoneGenericTypeVarBoundsAllows the bounds ofTypeVars to be generic.So you are able to have functions with polymorphic generic parameters.E=TypeVar("E")I=TypeVar("I",bound=Iterable[E])deffoo(i:I,e:E)->I:assertenotinireturnireveal_type(foo(["based"],"mypy"))# N: Revealed type is "list[str]"reveal_type(foo({1,2},3))# N: Revealed type is "set[int]"Overload Implementation InferenceThe types in overload implementations (including properties) can be inferred:@overloaddeff(a:int)->str:...@overloaddeff(a:str)->int:...deff(a):reveal_type(a)# int | strreturnNone# error: expected str | intclassA:@propertydeffoo(self)->int:...@foo.setterdeffoo(self,value):...# no need for annotationsInfer Function ParametersInfer the type of a function parameter from it’s default value:deff(a=1,b=True):reveal_type((a,b))# (int, bool)Tuple Literal TypesBasedmypy allows denotation of tuple types with tuple literals:a:(int,str)=(1,"a")Types in MessagesBasedmypy makes significant changes to error and info messages, consider:T=TypeVar("T",bound=int)deff(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"