Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use property decorator to support typing #2015

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
if TYPE_CHECKING:
from git.config import GitConfigParser
from git.objects.commit import Actor
from git.refs import Head, TagReference, RemoteReference, Reference
from git.refs.log import RefLogEntry
from git.repo import Repo

Expand Down Expand Up @@ -387,17 +386,23 @@ def set_object(
# set the commit on our reference
return self._get_reference().set_object(object, logmsg)

commit = property(
_get_commit,
set_commit, # type: ignore[arg-type]
doc="Query or set commits directly",
)
@property
def commit(self) -> "Commit":
"""Query or set commits directly"""
return self._get_commit()

@commit.setter
def commit(self, commit: Union[Commit, "SymbolicReference", str]) -> "SymbolicReference":
return self.set_commit(commit)

@property
def object(self) -> AnyGitObject:
"""Return the object our ref currently refers to"""
return self._get_object()

object = property(
_get_object,
set_object, # type: ignore[arg-type]
doc="Return the object our ref currently refers to",
)
@object.setter
def object(self, object: Union[AnyGitObject, "SymbolicReference", str]) -> "SymbolicReference":
return self.set_object(object)

def _get_reference(self) -> "SymbolicReference":
"""
Expand Down Expand Up @@ -496,12 +501,14 @@ def set_reference(
return self

# Aliased reference
reference: Union["Head", "TagReference", "RemoteReference", "Reference"]
reference = property( # type: ignore[assignment]
_get_reference,
set_reference, # type: ignore[arg-type]
doc="Returns the Reference we point to",
)
@property
def reference(self) -> "SymbolicReference":
return self._get_reference()

@reference.setter
def reference(self, ref: Union[AnyGitObject, "SymbolicReference", str]) -> "SymbolicReference":
return self.set_reference(ref)

ref = reference

def is_valid(self) -> bool:
Expand Down
40 changes: 21 additions & 19 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,21 +354,19 @@ def __ne__(self, rhs: object) -> bool:
def __hash__(self) -> int:
return hash(self.git_dir)

# Description property
def _get_description(self) -> str:
@property
def description(self) -> str:
"""The project's description"""
filename = osp.join(self.git_dir, "description")
with open(filename, "rb") as fp:
return fp.read().rstrip().decode(defenc)

def _set_description(self, descr: str) -> None:
@description.setter
def description(self, descr: str) -> None:
filename = osp.join(self.git_dir, "description")
with open(filename, "wb") as fp:
fp.write((descr + "\n").encode(defenc))

description = property(_get_description, _set_description, doc="the project's description")
del _get_description
del _set_description

@property
def working_tree_dir(self) -> Optional[PathLike]:
"""
Expand Down Expand Up @@ -885,13 +883,14 @@ def _set_daemon_export(self, value: object) -> None:
elif not value and fileexists:
os.unlink(filename)

daemon_export = property(
_get_daemon_export,
_set_daemon_export,
doc="If True, git-daemon may export this repository",
)
del _get_daemon_export
del _set_daemon_export
@property
def daemon_export(self) -> bool:
"""If True, git-daemon may export this repository"""
return self._get_daemon_export()

@daemon_export.setter
def daemon_export(self, value: object) -> None:
self._set_daemon_export(value)

def _get_alternates(self) -> List[str]:
"""The list of alternates for this repo from which objects can be retrieved.
Expand Down Expand Up @@ -929,11 +928,14 @@ def _set_alternates(self, alts: List[str]) -> None:
with open(alternates_path, "wb") as f:
f.write("\n".join(alts).encode(defenc))

alternates = property(
_get_alternates,
_set_alternates,
doc="Retrieve a list of alternates paths or set a list paths to be used as alternates",
)
@property
def alternates(self) -> List[str]:
"""Retrieve a list of alternates paths or set a list paths to be used as alternates"""
return self._get_alternates()

@alternates.setter
def alternates(self, alts: List[str]) -> None:
self._set_alternates(alts)

def is_dirty(
self,
Expand Down
Loading