Fix shadowed type annotations of builtins.list#41549
Fix shadowed type annotations of builtins.list#41549vbraun merged 5 commits intosagemath:developfrom
Conversation
|
Documentation preview for this PR (built with commit 4ee4a99; changes) is ready! 🎉 |
tobiasdiez
left a comment
There was a problem hiding this comment.
The linter fails now. What's the plan to follow the new rule? Deprecate the shadowing methods and provide new aliases for them?
I've turned off the rule in ruff. I thought the rule would only match type annotations, which are actually wrong, but it seems to also match names that are just stylistically not ideal. |
|
Enforcement for the type annotations should be something that a type checker can catch once we have more use of type annotations. I think the main issue with our type annotations right now is missing type stubs for Cython files. |
I think it makes sense to activate that rule (and fix the issues), as shadowing builtins is indeed not ideal (regardless of the circumstances). But that of course doesn't have to happen in this PR.
I agree fully! AI could help, but is hallucinating quite a bit (as the cython source files are usually too long). My idea was to generate the empty stubs (without types) using #41377, and then adding typing info using AI. What do you think? |
|
The current version of the PR looks good to me. Feel free to set it to positive review. |
As long as the shadowed names aren't in the global namespace I don't think it's a huge issue. So while I probably wouldn't have a method named
I think our first goal needs to be getting the whole codebase to pass a type checker with minimal settings. The main issue with that right now I think is the lack of I think that having types for the I can look at #41377 once it is ready. One thing I will say for a long complicated custom script like that: it needs to be documented well enough that others can work on it without having to ask the original author questions. |
Actually, many of our Cython methods don't have type infos for parameters. So it's very hard to autogen the pyi files with meaningful type infos. One approach could of course be to add typing first to the Cython files, which would likely have also other advantages in regard to performance and compile time errors.
Partly yes. Adding typing info to a method has two advantages:
I marked this PR now as ready for review. Incrementally improving the script as we add more typing info/run it on more cython files is probably a good strategy. |
sagemathgh-41549: Fix shadowed type annotations of builtins.list <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fix issues with type annotations when there is a method named `list` that shadows the name of `builtins.list`. See python/mypy#13050 for a description of the problem. Also ran ruff formatter and linter on the modified `.pyi` files and updated the mypy config while I was at it. Removed a few methods from the `.pyi` files that were only accessible in Cython and not accessible in Python. There are still some other issues with the `.pyi` files but this is an improvement at least. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#41549 Reported by: Vincent Macri Reviewer(s): Tobias Diez
sagemathgh-41549: Fix shadowed type annotations of builtins.list <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fix issues with type annotations when there is a method named `list` that shadows the name of `builtins.list`. See python/mypy#13050 for a description of the problem. Also ran ruff formatter and linter on the modified `.pyi` files and updated the mypy config while I was at it. Removed a few methods from the `.pyi` files that were only accessible in Cython and not accessible in Python. There are still some other issues with the `.pyi` files but this is an improvement at least. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#41549 Reported by: Vincent Macri Reviewer(s): Tobias Diez
Fix issues with type annotations when there is a method named
listthat shadows the name ofbuiltins.list. See python/mypy#13050 for a description of the problem.Also ran ruff formatter and linter on the modified
.pyifiles and updated the mypy config while I was at it. Removed a few methods from the.pyifiles that were only accessible in Cython and not accessible in Python. There are still some other issues with the.pyifiles but this is an improvement at least.📝 Checklist
⌛ Dependencies