lightly icon indicating copy to clipboard operation
lightly copied to clipboard

add mypy to pre-commit

Open gabrielfruet opened this issue 7 months ago • 3 comments

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:

  1. Fix mypy errors
  2. Add mypy to 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)

gabrielfruet avatar Apr 10 '26 13:04 gabrielfruet

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?

saud5150 avatar Jun 17 '26 07:06 saud5150

Thank you for the inputs. Could you verify if your mypy==1.4.1 ? I don't see those errors on my end.

gabrielfruet avatar Jun 17 '26 12:06 gabrielfruet

You're right, local master was behind upstream, sorry for the noise.

saud5150 avatar Jun 17 '26 17:06 saud5150