Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork939
Commite194d46
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
2 files changed
+11
-13
lines changedLines changed: 9 additions & 11 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
381 | 381 |
| |
382 | 382 |
| |
383 | 383 |
| |
384 |
| - | |
385 |
| - | |
386 |
| - | |
387 |
| - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
388 | 387 |
| |
389 | 388 |
| |
390 | 389 |
| |
391 |
| - | |
| 390 | + | |
392 | 391 |
| |
393 | 392 |
| |
394 | 393 |
| |
395 |
| - | |
396 |
| - | |
397 |
| - | |
398 |
| - | |
399 |
| - | |
400 |
| - | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
401 | 399 |
| |
402 | 400 |
| |
403 | 401 |
| |
|
Lines changed: 2 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
15 | 15 |
| |
16 | 16 |
| |
17 | 17 |
| |
18 |
| - | |
| 18 | + | |
19 | 19 |
| |
20 | 20 |
| |
21 | 21 |
| |
| |||
55 | 55 |
| |
56 | 56 |
| |
57 | 57 |
| |
58 |
| - | |
| 58 | + | |
59 | 59 |
| |
60 | 60 |
| |
61 | 61 |
| |
|
0 commit comments
Comments
(0)