Skip to content

Commit d481e6b

Browse files
committed
refactor(git): Replace __new__ override with explicit constructor
1 parent 035e0e0 commit d481e6b

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

src/dda/tools/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def get_remote_details(self, remote_name: str = "origin") -> Remote:
8585
["config", "--get", f"remote.{remote_name}.url"],
8686
).strip()
8787

88-
return Remote(remote_url) # type: ignore[abstract]
88+
return Remote.from_url(remote_url) # type: ignore[abstract]
8989

9090
def get_head_commit(self) -> Commit:
9191
"""

src/dda/utils/git/remote.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
class Remote(ABC):
1818
protocol: ClassVar[Literal["https", "git"]]
1919

20-
def __new__(cls, url: str) -> Remote: # noqa: PYI034
21-
if cls is Remote:
22-
if url.startswith("https://"):
23-
return HTTPSRemote(url)
24-
if url.startswith("git@"):
25-
return SSHRemote(url)
26-
msg = f"Invalid protocol: {url}"
27-
raise ValueError(msg)
28-
29-
return super().__new__(cls)
20+
@classmethod
21+
def from_url(cls, url: str) -> Remote:
22+
if url.startswith("https://"):
23+
return HTTPSRemote(url)
24+
if url.startswith("git@"):
25+
return SSHRemote(url)
26+
msg = f"Invalid protocol: {url}"
27+
raise ValueError(msg)
3028

3129
def __init__(self, url: str) -> None:
3230
self.url = url

tests/utils/git/test_commit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_details_github_git_equality(self, app, mocker):
9797
"dda.utils.network.http.client.HTTPClient.get",
9898
return_value=Response(status_code=200, content=github_payload_str),
9999
)
100-
github_details = commit.get_details_from_remote(Remote("https://github.com/foo/bar"))
100+
github_details = commit.get_details_from_remote(Remote.from_url("https://github.com/foo/bar"))
101101

102102
# Mock Git.capture to return payload from file
103103
git_output_file = Path(__file__).parent / "fixtures" / "git_show_dda_1425a34.txt"

tests/utils/git/test_github.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77

88

99
def test_get_github_url():
10-
remote = Remote(url="https://github.com/foo/bar")
10+
remote = Remote.from_url(url="https://github.com/foo/bar")
1111
assert get_github_url(remote) == "https://github.com/foo/bar"
1212

1313

1414
def test_get_github_api_url():
15-
remote = Remote(url="https://github.com/foo/bar")
15+
remote = Remote.from_url(url="https://github.com/foo/bar")
1616
assert get_github_api_url(remote) == "https://api.github.com/repos/foo/bar"
1717

1818

1919
def test_get_commit_github_url():
20-
remote = Remote(url="https://github.com/foo/bar")
20+
remote = Remote.from_url(url="https://github.com/foo/bar")
2121
sha1 = "1234567890" * 4
2222
commit = Commit(sha1=sha1)
2323
assert get_commit_github_url(remote, commit) == f"https://github.com/foo/bar/commit/{sha1}"
2424

2525

2626
def test_get_commit_github_api_url():
27-
remote = Remote(url="https://github.com/foo/bar")
27+
remote = Remote.from_url(url="https://github.com/foo/bar")
2828
sha1 = "1234567890" * 4
2929
commit = Commit(sha1=sha1)
3030
assert get_commit_github_api_url(remote, commit) == f"https://api.github.com/repos/foo/bar/commits/{sha1}"

tests/utils/git/test_remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TestRemoteClass:
3030
],
3131
)
3232
def test_basic(self, url):
33-
remote = Remote(url=url)
33+
remote = Remote.from_url(url=url)
3434
assert remote.url == url
3535
assert remote.org == "foo"
3636
assert remote.repo == "bar"
@@ -59,7 +59,7 @@ def test_get_commit_details_and_changes_from_remote(self, mocker, github_payload
5959
github_payload = json.loads(github_payload_str)
6060
sha1 = github_payload["sha"]
6161
commit_url = github_payload["commit"]["url"]
62-
remote = Remote(url=commit_url)
62+
remote = Remote.from_url(url=commit_url)
6363
commit = Commit(sha1=sha1)
6464

6565
# Create a CommitDetails object

0 commit comments

Comments
 (0)