Skip to content

Commit

Permalink
Merge pull request #1833 from timbrel/mypy-nits
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste authored Dec 8, 2023
2 parents 890e452 + 7033e06 commit 1a00172
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 72 deletions.
21 changes: 10 additions & 11 deletions core/commands/show_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from GitSavvy.github import github
from GitSavvy.github.git_mixins import GithubRemotesMixin
from GitSavvy.common import interwebs
SUBLIME_SUPPORTS_REGION_ANNOTATIONS = int(sublime.version()) >= 4050


__all__ = (
Expand All @@ -41,12 +40,13 @@
"GsShowCommitCopyCommitMessageHelper",
)

MYPY = False
if MYPY:
from typing import Dict, Optional, Sequence, Tuple, Union
from ..types import LineNo, ColNo
from GitSavvy.core.base_commands import GsCommand, Args, Kont

from typing import Dict, Optional, Sequence, Tuple, Union
from GitSavvy.core.base_commands import GsCommand, Args, Kont
from GitSavvy.core.types import LineNo, ColNo


SUBLIME_SUPPORTS_REGION_ANNOTATIONS = int(sublime.version()) >= 4050
SHOW_COMMIT_TITLE = "SHOW-COMMIT: {}"


Expand Down Expand Up @@ -309,8 +309,8 @@ def run(self, edit):
return

settings = view.settings()
file_path = settings.get("git_savvy.file_path")
commit_hash = settings.get("git_savvy.show_commit_view.commit")
file_path: Optional[str] = settings.get("git_savvy.file_path")
commit_hash: str = settings.get("git_savvy.show_commit_view.commit")

previous_commit = self.previous_commit(commit_hash, file_path)
if not previous_commit:
Expand Down Expand Up @@ -338,9 +338,8 @@ def run(self, edit):
return

settings = view.settings()
file_path = settings.get("git_savvy.file_path")
commit_hash = settings.get("git_savvy.show_commit_view.commit")

file_path: Optional[str] = settings.get("git_savvy.file_path")
commit_hash: str = settings.get("git_savvy.show_commit_view.commit")
next_commit = (
show_file_at_commit.recall_next_commit_for(view, commit_hash)
or self.next_commit(commit_hash, file_path)
Expand Down
74 changes: 28 additions & 46 deletions core/commands/show_file_at_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@
"gs_show_file_at_commit_open_graph_context",
)

MYPY = False
if MYPY:
from typing import Dict, Optional, Tuple

from typing import Dict, Optional, Tuple

SHOW_COMMIT_TITLE = "FILE: {}, {}"


def compute_identifier_for_view(view):
# type: (sublime.View) -> Optional[Tuple]
def compute_identifier_for_view(view: sublime.View) -> Optional[Tuple]:
settings = view.settings()
return (
settings.get('git_savvy.repo_path'),
Expand All @@ -44,8 +41,8 @@ def compute_identifier_for_view(view):

class gs_show_file_at_commit(WindowCommand, GitCommand):

def run(self, commit_hash, filepath, position=None, lang=None):
# type: (str, str, Optional[Position], Optional[str]) -> None
def run(self, commit_hash: str, filepath: str,
position: Optional[Position] = None, lang: Optional[str] = None) -> None:
this_id = (
self.repo_path,
filepath,
Expand All @@ -60,8 +57,8 @@ def run(self, commit_hash, filepath, position=None, lang=None):
else:
self.create_view(commit_hash, filepath, position, lang)

def create_view(self, commit_hash, file_path, position, syntax):
# type: (str, str, Optional[Position], Optional[str]) -> None
def create_view(self, commit_hash: str, file_path: str,
position: Optional[Position], syntax: Optional[str]) -> None:
active_view = self.window.active_view()
title = SHOW_COMMIT_TITLE.format(
os.path.basename(file_path),
Expand All @@ -85,8 +82,7 @@ def create_view(self, commit_hash, file_path, position, syntax):


class gs_show_file_at_commit_refresh(TextCommand, GitCommand):
def run(self, edit, position=None):
# type: (sublime.Edit, Position) -> None
def run(self, edit: sublime.Edit, position: Position = None) -> None:
view = self.view
settings = view.settings()
file_path = settings.get("git_savvy.file_path")
Expand All @@ -99,8 +95,7 @@ def run(self, edit, position=None):
self.update_status_bar(commit_hash)
enqueue_on_worker(self.update_reference_document, commit_hash, file_path)

def update_status_bar(self, commit_hash):
# type: (str) -> None
def update_status_bar(self, commit_hash: str) -> None:
short_hash = self.get_short_hash(commit_hash)
subject, date = self.commit_subject_and_date(commit_hash)
message = "On commit {}{}{}".format(short_hash, subject, date)
Expand All @@ -127,8 +122,7 @@ def sink(n=0):

sink()

def commit_subject_and_date(self, commit_hash):
# type: (str) -> Tuple[str, str]
def commit_subject_and_date(self, commit_hash: str) -> Tuple[str, str]:
# call with the same settings as gs_show_commit to either use or
# warm up the cache
show_diffstat = self.savvy_settings.get("show_diffstat")
Expand All @@ -137,29 +131,26 @@ def commit_subject_and_date(self, commit_hash):
date, subject = "", ""
for line in patch.splitlines():
# CommitDate: Tue Dec 20 18:21:40 2022 +0100
if line.startswith("CommitDate: "):
date_ = "-".join(map(str, email.utils.parsedate(line[12:])[:3]))
if line.startswith("CommitDate: ") and (parsed_date := email.utils.parsedate(line[12:])):
date_ = "-".join(map(str, parsed_date[:3]))
date = " ({})".format(date_)
elif line.startswith(" "):
subject_ = line.lstrip()
subject = ": {}".format(subject_)
break
return subject, date

def update_reference_document(self, commit_hash, file_path):
# type: (str, str) -> None
def update_reference_document(self, commit_hash: str, file_path: str) -> None:
self.view.set_reference_document(self.previous_file_version(commit_hash, file_path))

def update_title(self, commit_hash, file_path):
# type: (str, str) -> None
def update_title(self, commit_hash: str, file_path: str) -> None:
title = SHOW_COMMIT_TITLE.format(
os.path.basename(file_path),
self.get_short_hash(commit_hash),
)
self.view.set_name(title)

def previous_file_version(self, current_commit, file_path):
# type: (str, str) -> str
def previous_file_version(self, current_commit: str, file_path: str) -> str:
previous_commit = self.previous_commit(current_commit, file_path)
if previous_commit:
return self.get_file_content_at_commit(file_path, previous_commit)
Expand All @@ -170,16 +161,14 @@ def previous_file_version(self, current_commit, file_path):


@text_command
def render(view, text, position):
# type: (sublime.View, str, Optional[Position]) -> None
def render(view: sublime.View, text: str, position: Optional[Position]) -> None:
replace_view_content(view, text)
if position:
apply_position(view, *position)


class gs_show_file_at_commit_open_previous_commit(TextCommand, GitCommand):
def run(self, edit):
# type: (...) -> None
def run(self, edit) -> None:
view = self.view

settings = view.settings()
Expand All @@ -206,13 +195,12 @@ def run(self, edit):


class gs_show_file_at_commit_open_next_commit(TextCommand, GitCommand):
def run(self, edit):
# type: (...) -> None
def run(self, edit) -> None:
view = self.view

settings = view.settings()
file_path = settings.get("git_savvy.file_path")
commit_hash = settings.get("git_savvy.show_file_at_commit_view.commit")
file_path: str = settings.get("git_savvy.file_path")
commit_hash: str = settings.get("git_savvy.show_file_at_commit_view.commit")

next_commit = (
recall_next_commit_for(view, commit_hash)
Expand All @@ -236,29 +224,26 @@ def run(self, edit):
})


def remember_next_commit_for(view, mapping):
# type: (sublime.View, Dict[str, str]) -> None
def remember_next_commit_for(view: sublime.View, mapping: Dict[str, str]) -> None:
settings = view.settings()
store = settings.get("git_savvy.next_commits", {}) # type: Dict[str, str]
store: Dict[str, str] = settings.get("git_savvy.next_commits", {})
store.update(mapping)
settings.set("git_savvy.next_commits", store)


def recall_next_commit_for(view, commit_hash):
# type: (sublime.View, str) -> Optional[str]
def recall_next_commit_for(view: sublime.View, commit_hash: str) -> Optional[str]:
settings = view.settings()
store = settings.get("git_savvy.next_commits", {}) # type: Dict[str, str]
store: Dict[str, str] = settings.get("git_savvy.next_commits", {})
return store.get(commit_hash)


def pass_next_commits_info_along(view, to):
# type: (Optional[sublime.View], sublime.View) -> None
def pass_next_commits_info_along(view: Optional[sublime.View], to: sublime.View) -> None:
if not view:
return
from_settings, to_settings = view.settings(), to.settings()
if from_settings.get("git_savvy.file_path") != to_settings.get("git_savvy.file_path"):
return
store = from_settings.get("git_savvy.next_commits", {}) # type: Dict[str, str]
store: Dict[str, str] = from_settings.get("git_savvy.next_commits", {})
if store:
to_settings.set("git_savvy.next_commits", store)

Expand Down Expand Up @@ -293,8 +278,7 @@ def do_action(self, commit_hash, **kwargs):


class gs_show_file_at_commit_open_commit(TextCommand):
def run(self, edit):
# type: (...) -> None
def run(self, edit) -> None:
window = self.view.window()
if not window:
return
Expand All @@ -307,8 +291,7 @@ def run(self, edit):


class gs_show_file_at_commit_open_file_on_working_dir(TextCommand, GitCommand):
def run(self, edit):
# type: (...) -> None
def run(self, edit) -> None:
window = self.view.window()
if not window:
return
Expand All @@ -329,8 +312,7 @@ def run(self, edit):


class gs_show_file_at_commit_open_graph_context(TextCommand, GitCommand):
def run(self, edit):
# type: (...) -> None
def run(self, edit) -> None:
window = self.view.window()
if not window:
return
Expand Down
8 changes: 4 additions & 4 deletions core/git_mixins/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ def read_commit(
return rv

@cached(not_if={"current_commit": is_dynamic_ref})
def previous_commit(self, current_commit, file_path, follow=False):
# type: (Optional[str], str, bool) -> Optional[str]
def previous_commit(self, current_commit, file_path=None, follow=False):
# type: (Optional[str], Optional[str], bool) -> Optional[str]
try:
return self.git(
"log",
Expand All @@ -338,8 +338,8 @@ def previous_commit(self, current_commit, file_path, follow=False):
except IndexError:
return None

def next_commit(self, current_commit, file_path, follow=False):
# type: (str, str, bool) -> Optional[str]
def next_commit(self, current_commit, file_path=None, follow=False):
# type: (str, Optional[str], bool) -> Optional[str]
try:
return self.git(
"log",
Expand Down
14 changes: 6 additions & 8 deletions core/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

MYPY = False
if MYPY:
# Use LineNo, ColNo for 1-based line column counting (like git or `window.open_file`),
# use Row, Col for 0-based counting like Sublime's `view.rowcol`!
LineNo = int
ColNo = int
Row = int
Col = int
# Use LineNo, ColNo for 1-based line column counting (like git or `window.open_file`),
# use Row, Col for 0-based counting like Sublime's `view.rowcol`!
LineNo = int
ColNo = int
Row = int
Col = int
11 changes: 8 additions & 3 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

MYPY = False
if MYPY:
from typing import Callable, Dict, Iterator, Optional, Tuple, Type
from typing import Any, Callable, Dict, Iterator, Optional, Tuple, Type, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')


@contextmanager
Expand Down Expand Up @@ -429,12 +432,13 @@ def __setitem__(self, key, value):
self.popitem(last=False)


general_purpose_cache = Cache(maxsize=512) # type: Dict[Tuple, object]
general_purpose_cache = Cache(maxsize=512) # type: Dict[Tuple, Any]


def cached(not_if, cache=general_purpose_cache):
# type: (Dict, Dict[Tuple, object]) -> Callable
# type: (Dict[str, Callable], Dict[Tuple, Any]) -> Callable[[Callable[P, T]], Callable[P, T]]
def decorator(fn):
# type: (Callable[P, T]) -> Callable[P, T]
fn_s = inspect.signature(fn)

def should_skip(arguments):
Expand All @@ -445,6 +449,7 @@ def should_skip(arguments):

@wraps(fn)
def decorated(*args, **kwargs):
# type: (P.args, P.kwargs) -> T
arguments = _bind_arguments(fn_s, args, kwargs)
if should_skip(arguments):
return fn(*args, **kwargs)
Expand Down

0 comments on commit 1a00172

Please sign in to comment.