Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite194d46

Browse files
committed
Fix mypy warning "Missing return statement"
In git.index.base.IndexFile.from_tree, control was never able tofall off the end, but mypy reported a "Missing return statement"because it could not infer that the context manager didn't suppressany exceptions.This was for two reasons. An ExitStack context manager suppressesexceptions in some uses and not others, depending on the contextmanager objects its enter_context method is called with. In thiscase, that was sufficient to produce the mypy warning regardless ofother changes, so I converted it to nested with-statements. Thenesting depth is only 2, so this is not really any worse. (I alsoreworded the comments for clarity and adjusted their spacing.)The more important cause, however, could also produce this warningin code that uses GitPython, as git.index.util.TemporaryFileSwap ispublic. Its __exit__ method was annotated to return bool. This wasitself reported by mypy as an error, because a context manager__exit__ method that never suppresses exceptions should eitheralways (implicitly) return None and be annotated as such, or it canreturn False as this implementation does but should then have itsreturn type annotated as Literal[False].So this also changes TemporaryFileSwap.__exit__'s return typeannotation from bool to Literal[False]. If this were nonpublic orbeing newly designed, it may be better for it to instead returnNone implicitly, but I think that would technically be a breakingchange (though it would be very unusual, and not a good idea, forany code to ever rely on the distinction).With both these changes made, both those mypy warnings go away, andcode using GitPython will not get a warning if it makes similardirect use of TemporaryFileSwap.(An alternative might be to dedent the return statement out of thewith-statement in IndexFile.from_tree, but this would make lessclear to readers that it does not suppress exceptions. Furthermore,the change to TemporaryFileSwap.__exit__ would still have to bemade for mypy to consider it correctly typed.)
1 parent86d0177 commite194d46

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

‎git/index/base.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,23 +381,21 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
381381
arg_list.append("--aggressive")
382382
# END merge handling
383383

384-
# tmp file created in git home directory to be sure renaming
385-
# works - /tmp/ dirs could be on another device.
386-
withcontextlib.ExitStack()asstack:
387-
tmp_index=stack.enter_context(_named_temporary_file_for_subprocess(repo.git_dir))
384+
# Create the temporary file in the .git directory to be sure renaming
385+
# works - /tmp/ directories could be on another device.
386+
with_named_temporary_file_for_subprocess(repo.git_dir)astmp_index:
388387
arg_list.append("--index-output=%s"%tmp_index)
389388
arg_list.extend(treeish)
390389

391-
# Move current index out of the way - otherwise the merge may fail
390+
# Movethecurrent index out of the way - otherwise the merge may fail
392391
# as it considers existing entries. Moving it essentially clears the index.
393392
# Unfortunately there is no 'soft' way to do it.
394393
# The TemporaryFileSwap ensures the original file gets put back.
395-
396-
stack.enter_context(TemporaryFileSwap(join_path_native(repo.git_dir,"index")))
397-
repo.git.read_tree(*arg_list,**kwargs)
398-
index=cls(repo,tmp_index)
399-
index.entries# Force it to read the file as we will delete the temp-file.
400-
returnindex
394+
withTemporaryFileSwap(join_path_native(repo.git_dir,"index")):
395+
repo.git.read_tree(*arg_list,**kwargs)
396+
index=cls(repo,tmp_index)
397+
index.entries# Force it to read the file as we will delete the temp-file.
398+
returnindex
401399
# END index merge handling
402400

403401
# UTILITIES

‎git/index/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
fromtypingimportAny,Callable,TYPE_CHECKING,Optional,Type
1717

18-
fromgit.typesimportPathLike,_T
18+
fromgit.typesimportLiteral,PathLike,_T
1919

2020
ifTYPE_CHECKING:
2121
fromgit.indeximportIndexFile
@@ -55,7 +55,7 @@ def __exit__(
5555
exc_type:Optional[Type[BaseException]],
5656
exc_val:Optional[BaseException],
5757
exc_tb:Optional[TracebackType],
58-
)->bool:
58+
)->Literal[False]:
5959
ifosp.isfile(self.tmp_file_path):
6060
os.replace(self.tmp_file_path,self.file_path)
6161
returnFalse

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp