[LINT][PYTHON] Modernize annotations with ruff UP rules#18830
[LINT][PYTHON] Modernize annotations with ruff UP rules#18830tqchen merged 7 commits intoapache:mainfrom
Conversation
Summary of ChangesHello @tqchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on modernizing the codebase's Python annotations using ruff's pyupgrade rules, targeting Python 3.10. It also addresses technical debt by removing unnecessary future imports and streamlining import statements, resulting in a cleaner and more maintainable codebase. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a large-scale modernization of Python type annotations across the codebase, leveraging ruff's pyupgrade rules for Python 3.10. The changes consistently replace legacy typing constructs like typing.List, typing.Dict, typing.Optional, and typing.Union with their modern, built-in counterparts (e.g., list, dict, | operator). The Python version requirement has been correctly bumped to >=3.10 in pyproject.toml to support these changes. The automated refactoring appears to have been applied correctly and thoroughly, improving code readability and maintainability by adopting modern Python features. I have reviewed a substantial sample of the changes and found them to be correct and consistent.
39b30ef to
7c18bf7
Compare
This PR enables ruff pyupgrade (UP) rules with py310 target, auto-fixing ~5600 annotation modernizations (PEP 585 generics, PEP 604 unions, deprecated typing imports). Also removes from __future__ import annotations from ir/module.py and rmsnorm.py, bumps requires-python to >=3.10, and removes absolute_import aliases from topi/contrib files.
The ruff UP modernization converted typing.Union/Optional to PEP 604 syntax (X | Y, X | None), but the runtime type checker only recognized typing.Union. When a PEP 604 union like tuple[str, int] | None reached _type_check_atomic, isinstance() failed because it cannot handle parameterized generics. Fix: teach _Subtype.optional() and _Subtype.union() to also recognize types.UnionType (Python 3.10+ PEP 604 unions), and restore Union[types] in error reporting for _type_check_union.
`callable` (lowercase) is the builtin function, not a type — it doesn't support the `|` operator. Use `Callable` from `collections.abc` instead.
MSC code uses runtime-evaluated annotations with objects like multiprocessing.Manager (a function, not a type) and callable (builtin function), which break with PEP 604 X | None syntax. Add per-file-ignore for UP rules on MSC files and restore original annotations.
3627cbb to
a28e337
Compare
This PR enables ruff pyupgrade (UP) rules with py310 target, auto-fixing ~5600 annotation modernizations (PEP 585 generics, PEP 604 unions, deprecated typing imports).
Also removes from future import annotations from ir/module.py and rmsnorm.py, bumps requires-python to >=3.10, and removes absolute_import aliases from topi/contrib files.