add mypy to pre-commit
The code base currently has some mypy errors. Therefore, we can't simply add the mypy to the pre-commit hooks, because we wouldn't be able to commit due to the errors.
I'll open some PRs to:
- Fix
mypyerrors - Add
mypyto pre-commit
mypy errors:
#mypy lightly tests
lightly/models/modules/masked_causal_vision_transformer.py: note: In member "forward" of class "MaskedCausalAttention":
lightly/models/modules/masked_causal_vision_transformer.py:21: error: Signature of "forward" incompatible with supertype "Attention" [override]
lightly/models/modules/masked_causal_vision_transformer.py:21: note: Superclass:
lightly/models/modules/masked_causal_vision_transformer.py:21: note: def forward(self, x: Tensor, attn_mask: Tensor | None = ..., is_causal: bool = ...) -> Tensor
lightly/models/modules/masked_causal_vision_transformer.py:21: note: Subclass:
lightly/models/modules/masked_causal_vision_transformer.py:21: note: def forward(self, x: Tensor, mask: Tensor | None = ...) -> Tensor
lightly/models/modules/masked_causal_vision_transformer.py: note: In member "forward" of class "MaskedCausalBlock":
lightly/models/modules/masked_causal_vision_transformer.py:126: error: Signature of "forward" incompatible with supertype "Block" [override]
lightly/models/modules/masked_causal_vision_transformer.py:126: note: Superclass:
lightly/models/modules/masked_causal_vision_transformer.py:126: note: def forward(self, x: Tensor, attn_mask: Tensor | None = ..., is_causal: bool = ...) -> Tensor
lightly/models/modules/masked_causal_vision_transformer.py:126: note: Subclass:
lightly/models/modules/masked_causal_vision_transformer.py:126: note: def forward(self, x: Tensor, mask: Tensor | None = ...) -> Tensor
lightly/models/modules/masked_causal_vision_transformer.py: note: In member "forward_features" of class "MaskedCausalVisionTransformer":
lightly/models/modules/masked_causal_vision_transformer.py:170: error: Signature of "forward_features" incompatible with supertype "VisionTransformer" [override]
lightly/models/modules/masked_causal_vision_transformer.py:170: note: Superclass:
lightly/models/modules/masked_causal_vision_transformer.py:170: note: def forward_features(self, x: Tensor, attn_mask: Tensor | None = ..., is_causal: bool = ...) -> Tensor
lightly/models/modules/masked_causal_vision_transformer.py:170: note: Subclass:
lightly/models/modules/masked_causal_vision_transformer.py:170: note: def forward_features(self, x: Tensor, mask: Tensor | None = ...) -> Tensor
tests/loss/test_detcon_loss.py: note: In function "byol_nce_detcon":
tests/loss/test_detcon_loss.py:223: error: Incompatible types in assignment (expression has type "ndarray[tuple[Any, ...], dtype[float64]]", variable has type "ndarray[tuple[int, int], dtype[float64]]") [assignment]
Found 4 errors in 2 files (checked 530 source files)
Working on this. Implemented as a repo: local hook with language: system so it runs in the project's venv (mirrors-mypy won't work here — torch can't be listed as an additional_dependency).
The 4 errors listed in the issue are no longer present on master — looks like they've been fixed since this was filed. However, mypy lightly tests now surfaces 8 different errors across 4 files:
lightly/models/modules/ijepa_timm.py:94: error: Argument "norm_layer" to "Block" has incompatible type "Callable[..., Module]"; expected "type[Module]" [arg-type]
lightly/models/modules/masked_autoencoder_timm.py:92: error: Argument "norm_layer" to "Block" has incompatible type "Callable[..., Module]"; expected "type[Module]" [arg-type]
lightly/models/modules/masked_vision_transformer_timm.py:82: error: Statement is unreachable [unreachable]
lightly/models/modules/masked_vision_transformer_timm.py:199: error: Argument 1 to "normal_" has incompatible type "Optional[Parameter]"; expected "Tensor" [arg-type]
tests/loss/test_msn_loss.py:116: error: "Tensor" not callable [operator]
tests/loss/test_msn_loss.py:120: error: Argument 1 to "any" has incompatible type "Union[bool, Any]"; expected "Tensor" [arg-type]
tests/loss/test_msn_loss.py:138: error: "Tensor" not callable [operator]
tests/loss/test_msn_loss.py:142: error: Argument 1 to "any" has incompatible type "Union[bool, Any]"; expected "Tensor" [arg-type]
Would you like me to fix these in a separate PR first (matching your original plan), include them in the hook PR, or suppress them with targeted # type: ignore comments so the hook PR can land independently?
Thank you for the inputs. Could you verify if your mypy==1.4.1 ? I don't see those errors on my end.
You're right, local master was behind upstream, sorry for the noise.