Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork939
Commitc6f518b
committed
Keep mypy from thinking Git has arbitrary class attributes
The existence of __getattr__ or __getattribute__ on a class causesstatic type checkers like mypy to stop inferring that reads ofunrecognized instance attributes are static type errors. When theclass is a metaclass, this causes static type checkers to stopinferring that reads of unrecognized class attributes, on classesthat use (i.e., that have as their type) the metaclass, are statictype errors.The Git class itself defines __getattr__ and __getattribute__, butGit objects' instance attributes really are dynamically synthesized(in __getattr__). However, class attributes of Git are not dynamic,even though _GitMeta defines __getattribute__. Therefore, somethingspecial is needed so mypy infers nothing about Git class attributesfrom the existence of _GitMeta.__getattribute__.This takes the same approach as taken to the analogous problem with__getattr__ at module level, defining __getattribute__ with adifferent name first and then assigning that to __getattribute__under an `if not TYPE_CHECKING:`. (Allowing static type checkers tosee the original definition allows them to find some kinds of typeerrors in the body of the method, which is why the method isn'tjust defined in the `if not TYPE_CHECKING`.)Although it may not currently be necessary to hide __setattr__ too,the same is done with it in _GitMeta as well. The reason is thatthe intent may otherwise be subtly amgiguous to human readers andmaybe future tools: when __setattr__ exists, the effect of settinga class attribute that did not previously exist is in principleunclear, and might not make the attribute readble. (I am unsure ifthis is the right choice; either approach seems reasonable.)1 parent04eb09c commitc6f518b
1 file changed
+9
-2
lines changedLines changed: 9 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
335 | 335 |
| |
336 | 336 |
| |
337 | 337 |
| |
338 |
| - | |
| 338 | + | |
339 | 339 |
| |
340 | 340 |
| |
341 | 341 |
| |
342 | 342 |
| |
343 |
| - | |
| 343 | + | |
344 | 344 |
| |
345 | 345 |
| |
346 | 346 |
| |
347 | 347 |
| |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
348 | 355 |
| |
349 | 356 |
| |
350 | 357 |
| |
|
0 commit comments
Comments
(0)