-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing #5924
Typing #5924
Conversation
Sorry if this was discussed (I'm sure it was at some point). What is the benefit of having type information as separate |
Types inside .py-files have to wait till we drop python2 support. If I add types to .py files directly, it won't be possible to just cherry-pick new fixes into 2.9 patch-releases, because the code will contain py2-unsupported syntax and we'll need to manually update all cherry-picked changes. On the contrary, while we are keeping types in separate files, there is no extra-work for preparing patch releases. And no changes in the master branch will conflict with this WIP PR. When the most of codebase has type information and this PR is more or less ready to merge into master(or even after it becomes part of the master branch depending on the time it will take me to finish it), I can just quickly merge all .pyi files into .py and make final sanity check updating all the places that are out of sync. |
Comments are welcome. Implementation details
Next steps (not a part of this PR, contributors are welcome)
Run typechecker locally
|
@smotornyuk you spoke about the |
They are used when the type-checker doesn't have some extra information, that developer has. Two examples:
|
This PR will be merged on February, 21. Here's a short overview of features of your code editor, which will work better when it gets merged. This particular PR just specifies the type of variables used through the codebase of CKAN and doesn't provide any new features. Everything described below comes from your IDE/editor or, rather, from the specific language server that your editor uses. And you probably already know about these features. But just in case you've never used them, I'll add a few examples. This PR optimized for pyright. Here you can find some details about setting up pyright inside your editor. TLDR:
FeaturesAll screenshots are taken using VSCode with Code autocompletionYou probably had it before in most cases. But let's imagine you are writing Click's CLI command that uses @click.pass_context
def command(ctx):
ctx.a<NO AUTOCOMPLETION> Your editor may suggest some words based on the variables/functions available in the current buffer, but you cannot be sure that any of the suggestions actually exists inside But if specify that Logical checksSometimes, especially when refactoring the code, you get code blocks that have no sense. For example, you are checking the type of the variable that is guaranteed to be a string. Or you are using a variable that is initialized inside a conditional branch(i.e, possibly unbound variable). Finally, you can write a code that never executes because of an early return. Here's what it looks like: RefactoringIn the following code snippet, if you want to rename
But if you change the signature of the function to the following:
Your code editor will be able to rename all usages of the name property as long as it has a As soon as you set a new name for property inside this popup, all occurrences of And if you try to change the name of the |
Supply all
.py
files with corresponding.pyi
details.Initial stubs were generated by pyright because it puts a lot of extra information (like docstrings) into
.pyi
and it really helps to understand what's going on. Additionally, I've generated stubs using pytype(because it really shines when it comes to type inference) and now I'm going to compare these two versions + source code and add as much information as possible.Those types shouldn't be considered as a source of truth and it's ok if they won't be constantly synchronized with the codebase. In the near future, it will change a lot(because we are going to drop a lot of legacy code), so it's hardly possible to reflect all the changes on types. But these files will come in handy when we are ready to move all the types from
.pyi
into.py
and only after that, we can really rely on types in order to refactor functionality and find logical errors in code