From ec30065713cba61feb75fb6a9515787029adb32f Mon Sep 17 00:00:00 2001 From: herr kaste Date: Fri, 8 Dec 2023 14:20:59 +0100 Subject: [PATCH 1/3] nit: transform type comments to annotations --- core/commands/show_file_at_commit.py | 70 +++++++++++----------------- 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/core/commands/show_file_at_commit.py b/core/commands/show_file_at_commit.py index 7593f29da..e44e0a0d7 100644 --- a/core/commands/show_file_at_commit.py +++ b/core/commands/show_file_at_commit.py @@ -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'), @@ -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, @@ -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), @@ -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") @@ -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) @@ -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") @@ -146,20 +140,17 @@ def commit_subject_and_date(self, commit_hash): 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) @@ -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() @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 From b5c4691ed63fd5c65f82dcefacb05cf2f562fa77 Mon Sep 17 00:00:00 2001 From: herr kaste Date: Fri, 8 Dec 2023 21:22:31 +0100 Subject: [PATCH 2/3] Correctly type the `cached` decorator --- core/commands/show_file_at_commit.py | 4 ++-- core/utils.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core/commands/show_file_at_commit.py b/core/commands/show_file_at_commit.py index e44e0a0d7..335ccfdee 100644 --- a/core/commands/show_file_at_commit.py +++ b/core/commands/show_file_at_commit.py @@ -131,8 +131,8 @@ def commit_subject_and_date(self, commit_hash: str) -> Tuple[str, str]: 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() diff --git a/core/utils.py b/core/utils.py index 7c93a6bbb..1d20bb04d 100644 --- a/core/utils.py +++ b/core/utils.py @@ -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 @@ -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): @@ -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) From 7033e06107944574af80170fadb6af64cc7d85fd Mon Sep 17 00:00:00 2001 From: herr kaste Date: Fri, 8 Dec 2023 21:29:08 +0100 Subject: [PATCH 3/3] Clarify some types in `show_commit.py` --- core/commands/show_commit.py | 21 ++++++++++----------- core/git_mixins/history.py | 8 ++++---- core/types.py | 14 ++++++-------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/core/commands/show_commit.py b/core/commands/show_commit.py index f58046a52..69362826c 100644 --- a/core/commands/show_commit.py +++ b/core/commands/show_commit.py @@ -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__ = ( @@ -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: {}" @@ -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: @@ -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) diff --git a/core/git_mixins/history.py b/core/git_mixins/history.py index 12ad8caf3..5e547ef26 100644 --- a/core/git_mixins/history.py +++ b/core/git_mixins/history.py @@ -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", @@ -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", diff --git a/core/types.py b/core/types.py index 78eedb584..7f1b2a566 100644 --- a/core/types.py +++ b/core/types.py @@ -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