@@ -66,6 +66,7 @@ def __init__(self, context: CodemodContext) -> None:
66
66
self ._import_pydantic_validator = self ._import_pydantic_root_validator = False
67
67
self ._already_modified = False
68
68
self ._should_add_comment = False
69
+ self ._has_comment = False
69
70
self ._args : List [cst .Arg ] = []
70
71
71
72
@m .visit (IMPORT_VALIDATOR )
@@ -104,15 +105,17 @@ def visit_validator_decorator(self, node: cst.Decorator) -> None:
104
105
105
106
@m .visit (VALIDATOR_FUNCTION )
106
107
def visit_validator_func (self , node : cst .FunctionDef ) -> None :
108
+ for line in node .leading_lines :
109
+ if m .matches (line , m .EmptyLine (comment = m .Comment (value = CHECK_LINK_COMMENT ))):
110
+ self ._has_comment = True
107
111
# We are only able to refactor the `@validator` when the function has only `cls` and `v` as arguments.
108
112
if len (node .params .params ) > 2 :
109
113
self ._should_add_comment = True
110
114
111
115
@m .leave (ROOT_VALIDATOR_DECORATOR )
112
116
def leave_root_validator_func (self , original_node : cst .Decorator , updated_node : cst .Decorator ) -> cst .Decorator :
113
- for line in updated_node .leading_lines :
114
- if m .matches (line , m .EmptyLine (comment = m .Comment (value = CHECK_LINK_COMMENT ))):
115
- return updated_node
117
+ if self ._has_comment :
118
+ return updated_node
116
119
117
120
if self ._should_add_comment :
118
121
return self ._decorator_with_leading_comment (updated_node , ROOT_VALIDATOR_COMMENT )
@@ -121,9 +124,8 @@ def leave_root_validator_func(self, original_node: cst.Decorator, updated_node:
121
124
122
125
@m .leave (VALIDATOR_DECORATOR )
123
126
def leave_validator_decorator (self , original_node : cst .Decorator , updated_node : cst .Decorator ) -> cst .Decorator :
124
- for line in updated_node .leading_lines :
125
- if m .matches (line , m .EmptyLine (comment = m .Comment (value = CHECK_LINK_COMMENT ))):
126
- return updated_node
127
+ if self ._has_comment :
128
+ return updated_node
127
129
128
130
if self ._should_add_comment :
129
131
return self ._decorator_with_leading_comment (updated_node , VALIDATOR_COMMENT )
@@ -133,9 +135,11 @@ def leave_validator_decorator(self, original_node: cst.Decorator, updated_node:
133
135
@m .leave (VALIDATOR_FUNCTION | ROOT_VALIDATOR_FUNCTION )
134
136
def leave_validator_func (self , original_node : cst .FunctionDef , updated_node : cst .FunctionDef ) -> cst .FunctionDef :
135
137
self ._args = []
138
+ self ._has_comment = False
136
139
if self ._should_add_comment :
137
140
self ._should_add_comment = False
138
141
return updated_node
142
+
139
143
classmethod_decorator = cst .Decorator (decorator = cst .Name ("classmethod" ))
140
144
return updated_node .with_changes (decorators = [* updated_node .decorators , classmethod_decorator ])
141
145
0 commit comments