If your function just wraps another you can use the same type hints as the other function with functools.wraps https://docs.python.org/3/library/functools.html#functools.w...
If your function just wraps another you can use the same type hints as the other function with functools.wraps https://docs.python.org/3/library/functools.html#functools.w...
Underneath, this is because many (most?) type checkers for Python aren't actually running the code in order to access annotation information, and are instead parsing it "from the outside" using complex and fallible techniques of variable reliability. That said, it's a testament to JetBrains' excellent work that PyCharm's checker works as well as it does, given how crazily metaprogrammed even simple Python often turns out to be.
I recently switched from Pycharm to vscode which uses pyright and it's night and day on the amount of type errors it catches, it considerably improved the quality of my code and confidence during refactoring.
And to add insult to injury Pycharm doesn't even have a pyright plugin and the mypy plugin is extremely slow and buggy.
Their Python support has t kept up with other tools as noted. I’ve see a similar decline in the ability for for them to keep up to date with things like Svelte, Vue, Astro etc too.
They need to embrace the LSP
IntelliJ (the Java+ IDE) always has a community edition that is open source. I can vouch that it is truly free and not crippleware. For most Java programmer, this edition is sufficient.
When I first learned about LSPs it was immediately clear to me they would run circles around the "traditional" IDEs. I'd given up on using IDEs because I found them too finicky and error prone, but LSPs have been a total game changer.
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"https://docs.python.org/3/library/typing.html#typing.ParamSp...
Not ideal but not too bad either.