|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from typing import Set |
| 7 | + |
| 8 | +import libcst |
| 9 | + |
| 10 | +from fixit import Invalid, LintRule, Valid |
| 11 | + |
| 12 | + |
| 13 | +REPLACE_TYPING_TYPE_ANNOTATION: str = ( |
| 14 | + "Use lowercase primitive type {primitive_type}" |
| 15 | + + "instead of {typing_type} (See [PEP 585 – Type Hinting Generics In Standard Collections](https://peps.python.org/pep-0585/#forward-compatibility))" |
| 16 | +) |
| 17 | + |
| 18 | +CUSTOM_TYPES_TO_REPLACE: Set[str] = {"Dict", "List", "Set", "Tuple"} |
| 19 | + |
| 20 | + |
| 21 | +class UsePrimitiveTypes(LintRule): |
| 22 | + """ |
| 23 | + Enforces the use of primitive types instead of those in the ``typing`` module () |
| 24 | + since they are available on and ahead of Python ``3.10``. |
| 25 | + """ |
| 26 | + |
| 27 | + PYTHON_VERSION = ">= 3.10" |
| 28 | + |
| 29 | + VALID = [ |
| 30 | + Valid( |
| 31 | + """ |
| 32 | + def foo() -> list: |
| 33 | + pass |
| 34 | + """, |
| 35 | + ), |
| 36 | + Valid( |
| 37 | + """ |
| 38 | + def bar(x: set) -> None: |
| 39 | + pass |
| 40 | + """, |
| 41 | + ), |
| 42 | + Valid( |
| 43 | + """ |
| 44 | + def baz(y: tuple) -> None: |
| 45 | + pass |
| 46 | + """, |
| 47 | + ), |
| 48 | + Valid( |
| 49 | + """ |
| 50 | + def qux(z: dict) -> None: |
| 51 | + pass |
| 52 | + """, |
| 53 | + ), |
| 54 | + ] |
| 55 | + |
| 56 | + INVALID = [ |
| 57 | + Invalid( |
| 58 | + """ |
| 59 | + def foo() -> List[int]: |
| 60 | + pass |
| 61 | + """, |
| 62 | + expected_replacement=""" |
| 63 | + def foo() -> list[int]: |
| 64 | + pass |
| 65 | + """, |
| 66 | + ), |
| 67 | + Invalid( |
| 68 | + """ |
| 69 | + def bar(x: Set[str]) -> None: |
| 70 | + pass |
| 71 | + """, |
| 72 | + expected_replacement=""" |
| 73 | + def bar(x: set[str]) -> None: |
| 74 | + pass |
| 75 | + """, |
| 76 | + ), |
| 77 | + Invalid( |
| 78 | + """ |
| 79 | + def baz(y: Tuple[int, str]) -> None: |
| 80 | + pass |
| 81 | + """, |
| 82 | + expected_replacement=""" |
| 83 | + def baz(y: tuple[int, str]) -> None: |
| 84 | + pass |
| 85 | + """, |
| 86 | + ), |
| 87 | + Invalid( |
| 88 | + """ |
| 89 | + def qux(z: Dict[str, int]) -> None: |
| 90 | + pass |
| 91 | + """, |
| 92 | + expected_replacement=""" |
| 93 | + def qux(z: dict[str, int]) -> None: |
| 94 | + pass |
| 95 | + """, |
| 96 | + ), |
| 97 | + ] |
| 98 | + |
| 99 | + def __init__(self) -> None: |
| 100 | + super().__init__() |
| 101 | + self.annotation_counter: int = 0 |
| 102 | + |
| 103 | + def visit_Annotation(self, node: libcst.Annotation) -> None: |
| 104 | + self.annotation_counter += 1 |
| 105 | + |
| 106 | + def leave_Annotation(self, original_node: libcst.Annotation) -> None: |
| 107 | + self.annotation_counter -= 1 |
| 108 | + |
| 109 | + def visit_FunctionDef(self, node: libcst.FunctionDef) -> None: |
| 110 | + # Check return type |
| 111 | + if isinstance(node.returns, libcst.Annotation): |
| 112 | + if isinstance(node.returns.annotation, libcst.Subscript): |
| 113 | + base_type = node.returns.annotation.value |
| 114 | + if ( |
| 115 | + isinstance(base_type, libcst.Name) |
| 116 | + and base_type.value in CUSTOM_TYPES_TO_REPLACE |
| 117 | + ): |
| 118 | + new_base_type = base_type.with_changes( |
| 119 | + value=base_type.value.lower() |
| 120 | + ) |
| 121 | + new_annotation = node.returns.annotation.with_changes( |
| 122 | + value=new_base_type |
| 123 | + ) |
| 124 | + new_returns = node.returns.with_changes(annotation=new_annotation) |
| 125 | + new_node = node.with_changes(returns=new_returns) |
| 126 | + self.report( |
| 127 | + node, |
| 128 | + REPLACE_TYPING_TYPE_ANNOTATION.format( |
| 129 | + primitive_type=base_type.value.lower(), |
| 130 | + typing_type=base_type.value, |
| 131 | + ), |
| 132 | + replacement=new_node, |
| 133 | + ) |
| 134 | + |
| 135 | + # Check parameter types |
| 136 | + for param in node.params.params: |
| 137 | + if isinstance(param.annotation, libcst.Annotation): |
| 138 | + if isinstance(param.annotation.annotation, libcst.Subscript): |
| 139 | + base_type = param.annotation.annotation.value |
| 140 | + if ( |
| 141 | + isinstance(base_type, libcst.Name) |
| 142 | + and base_type.value in CUSTOM_TYPES_TO_REPLACE |
| 143 | + ): |
| 144 | + new_base_type = base_type.with_changes( |
| 145 | + value=base_type.value.lower() |
| 146 | + ) |
| 147 | + new_annotation = param.annotation.annotation.with_changes( |
| 148 | + value=new_base_type |
| 149 | + ) |
| 150 | + new_param_annotation = param.annotation.with_changes( |
| 151 | + annotation=new_annotation |
| 152 | + ) |
| 153 | + new_param = param.with_changes(annotation=new_param_annotation) |
| 154 | + self.report( |
| 155 | + param, |
| 156 | + REPLACE_TYPING_TYPE_ANNOTATION.format( |
| 157 | + primitive_type=base_type.value.lower(), |
| 158 | + typing_type=base_type.value, |
| 159 | + ), |
| 160 | + replacement=new_param, |
| 161 | + ) |
0 commit comments