def foo() -> int:
pass
I sure hope they improved the type checker in later versions... def foo() -> int:
pass
I sure hope they improved the type checker in later versions...https://mypy.readthedocs.io/en/stable/stubs.html#using-stub-...
No it isn't. The accepted stub syntax is an ellipses (...).
A competent type checker like pyright will error on this code.
Function with declared return type "int" must return value on all code paths
Type "None" cannot be assigned to type "int"
And if you need more proof that pycharm is useless as a type checker: def foo(b) -> int:
if b:
return 1
A classic error where you forget to return a value from all paths and pycharm is silent.>(Using stub file syntax at runtime). You may also occasionally need to elide actual logic in regular Python code... You can also elide default arguments as long as the function body also contains no runtime logic: the function body only contains a single ellipsis, the pass statement, or a raise NotImplementedError(). It is also acceptable for the function body to contain a docstring. For example:
In any case, the second example is definitely not a stub.
>And to add insult to injury Pycharm doesn't even have a pyright plugin and the mypy plugin is extremely slow and buggy.
ie you recognize that aligning with mypy is desirable.
For example, try:
a : int = None
It will succeed. This is done (I think) so you can tell whether optional arguments are defined, declare variables before use (eg if you have a conditional with two branches both setting a different value for a variable sibce python blocks aren’t expressions) and that kind of thing. Expression of type "None" cannot be assigned to declared type "int"
Type "None" cannot be assigned to type "int"