A function or class is defined again before the first definition is used
What it finds
A function or class defined again in the same block before anything used the first definition: the first one is replaced without ever being called, which usually means a copied test or method kept its old name.
The first definition must have no decorators, since a decorator such as @overload, @property or a registration can make use of it. Any mention of the name between the two definitions, including in the decorators of the second, counts as a use, and so does a mention in a function defined in between, which may run later. Scripts split into notebook cells by # %% comments are run cell by cell, so a definition can be used interactively before the next cell replaces it; definitions in different cells are left alone.
From the rule's source, redefined_function.rs.
from typing import overload def helper(): return 1 def helper():tx0 reports: the function helper is defined again, and the earlier definition is never used return 2 class TestParser: def test_empty(self): pass def test_empty(self):tx0 reports: the function test_empty is defined again pass @property def size(self): return 1 @size.setter def size(self, value): pass class Model: pass class Model:tx0 reports: the class Model is defined again pass def used(): return 1 first = used def used(): return 2 def late(): return 1 def caller(): return late() def late(): return 2 @overloaddef parse(x: int) -> int: ...@overloaddef parse(x: str) -> str: ...def parse(x): return x if first: def choice(): return 1else: def choice(): return 2 def cell(): return 1 # %% def cell(): return 2 Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/python:redefined-function" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:redefined-function] reason="..."tx0 explain tx0/python:redefined-function shows the same in the terminal. Found a false positive? Report it.