Skip to content

Commit 4465aec

Browse files
authored
Drop replace methods with builtin (#2646)
## Changes Our code implements 'replace' methods on some dataclasses with no benefit over the builtin method. This PR fixes that. ### Linked issues None ### Functionality None ### Tests - [x] ran unit tests --------- Co-authored-by: Eric Vergnaud <[email protected]>
1 parent d113e3e commit 4465aec

File tree

7 files changed

+24
-49
lines changed

7 files changed

+24
-49
lines changed

src/databricks/labs/ucx/source_code/base.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import codecs
4+
import dataclasses
45
import locale
56
import logging
67
from abc import abstractmethod, ABC
@@ -41,25 +42,6 @@ class Advice:
4142
end_line: int
4243
end_col: int
4344

44-
def replace(
45-
self,
46-
*,
47-
code: str | None = None,
48-
message: str | None = None,
49-
start_line: int | None = None,
50-
start_col: int | None = None,
51-
end_line: int | None = None,
52-
end_col: int | None = None,
53-
) -> Advice:
54-
return type(self)(
55-
code=code if code is not None else self.code,
56-
message=message if message is not None else self.message,
57-
start_line=start_line if start_line is not None else self.start_line,
58-
start_col=start_col if start_col is not None else self.start_col,
59-
end_line=end_line if end_line is not None else self.end_line,
60-
end_col=end_col if end_col is not None else self.end_col,
61-
)
62-
6345
def as_advisory(self) -> 'Advisory':
6446
return Advisory(**self.__dict__)
6547

@@ -89,7 +71,8 @@ def from_node(cls, *, code: str, message: str, node: NodeNG) -> Advice:
8971

9072
def replace_from_node(self, node: NodeNG) -> Advice:
9173
# Astroid lines are 1-based.
92-
return self.replace(
74+
return dataclasses.replace(
75+
self,
9376
start_line=(node.lineno or 1) - 1,
9477
start_col=node.col_offset,
9578
end_line=(node.end_lineno or 1) - 1,

src/databricks/labs/ucx/source_code/graph.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import abc
4+
import dataclasses
45
import itertools
56
import logging
67
from dataclasses import dataclass
@@ -95,7 +96,8 @@ def register_dependency(self, dependency: Dependency) -> MaybeGraph:
9596
return MaybeGraph(
9697
child_graph,
9798
[
98-
problem.replace(
99+
dataclasses.replace(
100+
problem,
99101
source_path=dependency.path if problem.is_path_missing() else problem.source_path,
100102
)
101103
for problem in problems
@@ -473,7 +475,7 @@ def _make_relative_paths(self, problems: list[DependencyProblem], path: Path) ->
473475
out_path = path if problem.is_path_missing() else problem.source_path
474476
if out_path.is_absolute() and out_path.is_relative_to(self._path_lookup.cwd):
475477
out_path = out_path.relative_to(self._path_lookup.cwd)
476-
adjusted_problems.append(problem.replace(source_path=out_path))
478+
adjusted_problems.append(dataclasses.replace(problem, source_path=out_path))
477479
return adjusted_problems
478480

479481
def __repr__(self):
@@ -497,26 +499,6 @@ class DependencyProblem:
497499
def is_path_missing(self):
498500
return self.source_path == Path(MISSING_SOURCE_PATH)
499501

500-
def replace(
501-
self,
502-
code: str | None = None,
503-
message: str | None = None,
504-
source_path: Path | None = None,
505-
start_line: int | None = None,
506-
start_col: int | None = None,
507-
end_line: int | None = None,
508-
end_col: int | None = None,
509-
) -> DependencyProblem:
510-
return DependencyProblem(
511-
code if code is not None else self.code,
512-
message if message is not None else self.message,
513-
source_path if source_path is not None else self.source_path,
514-
start_line if start_line is not None else self.start_line,
515-
start_col if start_col is not None else self.start_col,
516-
end_line if end_line is not None else self.end_line,
517-
end_col if end_col is not None else self.end_col,
518-
)
519-
520502
def as_advisory(self) -> 'Advisory':
521503
return Advisory(
522504
code=self.code,

src/databricks/labs/ucx/source_code/notebooks/cells.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import dataclasses
34
import logging
45
import re
56
import shlex
@@ -105,8 +106,10 @@ def build_dependency_graph(self, parent: DependencyGraph) -> list[DependencyProb
105106
python_dependency_problems = analyzer.build_graph()
106107
# Position information for the Python code is within the code and needs to be mapped to the location within the parent nodebook.
107108
return [
108-
problem.replace(
109-
start_line=self.original_offset + problem.start_line, end_line=self.original_offset + problem.end_line
109+
dataclasses.replace(
110+
problem,
111+
start_line=self.original_offset + problem.start_line,
112+
end_line=self.original_offset + problem.end_line,
110113
)
111114
for problem in python_dependency_problems
112115
]
@@ -177,7 +180,7 @@ def build_dependency_graph(self, parent: DependencyGraph) -> list[DependencyProb
177180
start_line = self._original_offset + idx
178181
problems = parent.register_notebook(path, True)
179182
return [
180-
problem.replace(start_line=start_line, start_col=0, end_line=start_line, end_col=len(line))
183+
dataclasses.replace(problem, start_line=start_line, start_col=0, end_line=start_line, end_col=len(line))
181184
for problem in problems
182185
]
183186
start_line = self._original_offset

src/databricks/labs/ucx/source_code/notebooks/sources.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import dataclasses
34
import locale
45
import logging
56
from collections.abc import Iterable
@@ -173,7 +174,8 @@ def lint(self) -> Iterable[Advice]:
173174
linter = self._linter(cell.language.language)
174175
advices = linter.lint(cell.original_code)
175176
for advice in advices:
176-
yield advice.replace(
177+
yield dataclasses.replace(
178+
advice,
177179
start_line=advice.start_line + cell.original_offset,
178180
end_line=advice.end_line + cell.original_offset,
179181
)

src/databricks/labs/ucx/source_code/python/python_analyzer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import dataclasses
34
import logging
45
from collections.abc import Iterable
56
from pathlib import Path
@@ -48,7 +49,8 @@ def build_graph(self) -> list[DependencyProblem]:
4849
for base_node in nodes:
4950
for problem in self._build_graph_from_node(base_node):
5051
# Astroid line numbers are 1-based.
51-
problem = problem.replace(
52+
problem = dataclasses.replace(
53+
problem,
5254
start_line=base_node.node.lineno - 1,
5355
start_col=base_node.node.col_offset,
5456
end_line=(base_node.node.end_lineno or 1) - 1,

tests/unit/source_code/test_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import dataclasses
2+
13
from databricks.labs.ucx.source_code.base import (
24
Advice,
35
Advisory,
@@ -20,7 +22,7 @@ def test_message_initialization():
2022
def test_warning_initialization():
2123
warning = Advisory('code2', 'This is a warning', 1, 1, 2, 2)
2224

23-
copy_of = warning.replace(code='code3')
25+
copy_of = dataclasses.replace(warning, code='code3')
2426
assert copy_of.code == 'code3'
2527
assert isinstance(copy_of, Advisory)
2628

tests/unit/source_code/test_s3fs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dataclasses
12
from pathlib import Path
23

34
import pytest
@@ -126,7 +127,7 @@ def test_detect_s3fs_import(empty_index, source: str, expected: list[DependencyP
126127
pip_resolver, notebook_resolver, import_resolver, import_resolver, mock_path_lookup
127128
)
128129
maybe = dependency_resolver.build_local_file_dependency_graph(sample, CurrentSessionState())
129-
assert maybe.problems == [_.replace(source_path=sample) for _ in expected]
130+
assert maybe.problems == [dataclasses.replace(_, source_path=sample) for _ in expected]
130131

131132

132133
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)