(
self,
node: nodes.NodeType, # original AST node from the source code
other: nodes.NodeType # AST node pattern to match against
)
| 213 | self.pattern = pattern |
| 214 | |
| 215 | def match( |
| 216 | self, |
| 217 | node: nodes.NodeType, # original AST node from the source code |
| 218 | other: nodes.NodeType # AST node pattern to match against |
| 219 | ) -> bool: |
| 220 | if type(other) == ASTPattern.AnyOf: |
| 221 | return self._match_any_of(node, other) |
| 222 | elif node is None: |
| 223 | return other is None |
| 224 | elif type(node) == bool: |
| 225 | if type(other) != bool: |
| 226 | return False |
| 227 | return node is other |
| 228 | elif type(node) == str: |
| 229 | if type(other) not in (str, nodes.String): |
| 230 | return False |
| 231 | return node == str(other) |
| 232 | elif type(node) == dict: |
| 233 | return self._match_dict(node, other) |
| 234 | elif type(node) in (list, tuple): |
| 235 | return self._match_list(node, other) |
| 236 | elif type(node) == int: |
| 237 | if type(other) not in (int, nodes.Number): |
| 238 | return False |
| 239 | return node == int(other) |
| 240 | elif type(node) == float: |
| 241 | if type(other) != float: |
| 242 | return False |
| 243 | return node == other |
| 244 | |
| 245 | try: |
| 246 | return node.match(other, self) |
| 247 | except AttributeError: |
| 248 | return False |
| 249 | |
| 250 | # Dispatch method for basic python types |
| 251 | def _match_dict(self, node: dict, other) -> bool: |
no test coverage detected