modules checked against their stubs¶
a module with a stub beside it (.pyi or .byi, a foo-stubs package, or the
standard library's own stubs) is typed by the stub wherever it is imported, so the
stub is what the module promises its users. basedpython holds the module's own
body to that promise, in .py and .by files alike
what the stub declares, the body is checked against¶
a parameter, a return type, a module variable or a class attribute the module leaves unannotated takes the type the stub declares for it:
# shapes.pyi
class Square:
side: int
def scale(self, factor: int) -> "Square": ...
def area(side: int) -> int: ...
# shapes.py
def area(side):
reveal_type(side) # int
return str(side * side) # error: `str` is not `int`
class Square:
def __init__(self):
self.side = "1" # error: `str` is not `int`
def scale(self, factor):
reveal_type(factor) # int
square = Square()
square.side = self.side * factor
return square
- a function the stub overloads is the implementation of every overload, as an implementation written beside its overloads is: a parameter takes the union of what the overloads say, and so does the return type
- of a
sys.version_infoorsys.platformbranch in the stub, the one that runs is read - an annotation the module writes is what its body is checked against. where it disagrees with the stub, that is reported as below
- the stub's types are read in the module's terms. a class the stub defines is the
module's class of the same name (above,
scalereturns the module'sSquare),Selfin a stub method is the module's method's, a type variable a stub function binds makes the module's function generic over it, and one a stub class binds is the module's class's type parameter in the same place - a type naming something only the stub has is not carried over: a class the
module doesn't define, a type variable of a class the module doesn't make
generic, an enum member or a
NewTypeof the stub's own
importers see the stub as they always did; nothing here changes what they read
the editor's stubTypes hints show the stub's annotations where the module leaves
them out, and say exactly what the checker reads there
where the module and its stub disagree¶
| rule | default | reports |
|---|---|---|
missing-runtime-member |
warn | a member the stub declares and the module (or class) lacks |
incompatible-stub-signature |
error | a call the stub accepts that the function can't take, or an annotation that disagrees with the stub's |
incompatible-stub-attribute |
error | a variable annotated with another type than the stub's |
incompatible-stub-bases |
error | a class that doesn't inherit from a base the stub lists |
missing-stub-member |
off | a public member of the module the stub leaves out |
a member the stub declares only for a type checker is not expected of the module:
private names, anything @type_check_only, type variables, type aliases,
__all__, and a __getattr__ that says the stub lists only some attributes. nor is anything a module- or class-level __getattr__ could supply, or
a member a class inherits. missing-runtime-member offers to write the member in
as the stub declares it
missing-stub-member is off because a stub often describes only the supported
part of a module; turn it on for a module whose stub is meant to be complete