Skip to content

Include mypy in run_all_tests #2670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 12, 2022
Merged

Include mypy in run_all_tests #2670

merged 3 commits into from
Dec 12, 2022

Conversation

zaxtax
Copy link
Collaborator

@zaxtax zaxtax commented Nov 29, 2022

This continues work on #688 to include mypy in testing

Checklist

  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other
    checks if that's the case).
  • This change is discussed in a Github issue/
    discussion (please add a
    link).
  • The documentation and docstrings adhere to the
    documentation guidelines.
  • This change includes necessary high-coverage tests.
    (No quality testing = no merge!)

@zaxtax zaxtax requested a review from cgarciae November 29, 2022 17:42
@zaxtax zaxtax force-pushed the mypy_build branch 5 times, most recently from 4347631 to 07a8e9d Compare December 6, 2022 17:25
@zaxtax zaxtax marked this pull request as ready for review December 6, 2022 17:50
@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2022

Codecov Report

Merging #2670 (5b809bb) into main (09b6d56) will increase coverage by 0.02%.
The diff coverage is 76.31%.

@@            Coverage Diff             @@
##             main    #2670      +/-   ##
==========================================
+ Coverage   81.15%   81.17%   +0.02%     
==========================================
  Files          51       51              
  Lines        5492     5493       +1     
==========================================
+ Hits         4457     4459       +2     
+ Misses       1035     1034       -1     
Impacted Files Coverage Δ
flax/core/nn/attention.py 13.47% <0.00%> (ø)
flax/core/nn/linear.py 42.85% <0.00%> (ø)
flax/io.py 85.26% <50.00%> (ø)
flax/linen/attention.py 95.96% <50.00%> (ø)
flax/core/meta.py 98.64% <66.66%> (ø)
flax/core/scope.py 90.13% <75.00%> (ø)
flax/training/checkpoints.py 66.14% <84.61%> (+0.26%) ⬆️
flax/core/frozen_dict.py 96.22% <100.00%> (ø)
flax/linen/dotgetter.py 72.91% <100.00%> (ø)
flax/linen/linear.py 97.51% <100.00%> (ø)
... and 5 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@levskaya levskaya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments below.

Just curious - does modern mypy use mypyc compiler by default or is that a random side-effort?

@@ -32,7 +32,7 @@
from jax.experimental import pjit


TAxisMetadata = TypeVar('TAxisMetadata', bound='AxisMetadata')
TAxisMetadata = Any # TypeVar('TAxisMetadata', bound='AxisMetadata')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy doesn't support bound type-vars?! is this intentional Any cast?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy has had lots of trouble dealing with type-vars in general. I've tried to preserve their use, and just define them as Any. That way, we can reintroduce stricter checking later where possible.

@@ -30,10 +30,10 @@

# Type annotations
Array = jnp.ndarray
Axes = Union[int, Iterable[int]]
DType = jnp.dtype
Axes = Union[int, Any]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a IntSequence (or something) to indicate what this Any is supposed to be for a human reader?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a great idea! Just as an FYI, there are many places in the code where pytype wanted something to be Iterabale and mypy wanted something to be Sequence. I couldn't make both happy so they ended up with Any

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's both sad and funny... I wonder if there's a way to harmonize these two in our code better or to agitate for change - for now just another Any alias indicating we intend a sequence of ints should be ok

@@ -1559,7 +1559,7 @@ def loss(params, perturbations, inputs, targets):
# [-1.456924 -0.44332537 0.02422847]]

"""
value += self.variable(collection, name, lambda: jnp.zeros_like(value)).value
value += self.variable(collection, name, lambda: jnp.zeros_like(value)).value # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we messing up internal type annotations in flax module methods to require this opt-out?

@levskaya
Copy link
Collaborator

levskaya commented Dec 7, 2022

when we add new open source config files we need to configure them to be sync'd by the internal sync system first - I just added your mypy.ini file to #2693 - maybe rebase on that after it goes in (hopefully soon).

@zaxtax
Copy link
Collaborator Author

zaxtax commented Dec 7, 2022 via email

@levskaya
Copy link
Collaborator

levskaya commented Dec 7, 2022

@zaxtax - seeing a small number of new internal pytype bugs of this sort:

File "rax/examples/flax_integration/main.py", line 143, in train_step: Function FrozenDict.copy was called with the wrong arguments [wrong-arg-types]
         Expected: (self, add_or_replace: flax.core.frozen_dict.FrozenDict[str, Mapping[str, Any]])
  Actually passed: (self, add_or_replace: Dict[str, Any])

File "jestimator/states.py", line 73, in variables: Function FrozenDict.copy was called with the wrong arguments [wrong-arg-types]
         Expected: (self, add_or_replace: flax.core.frozen_dict.FrozenDict[str, Any])
  Actually passed: (self, add_or_replace: Dict[str, Optional[flax.core.frozen_dict.FrozenDict[str, Any]]])

File "jestimator/states.py", line 143, in variables: Function FrozenDict.copy was called with the wrong arguments [wrong-arg-types]
         Expected: (self, add_or_replace: flax.core.frozen_dict.FrozenDict[str, Any])
  Actually passed: (self, add_or_replace: Dict[str, Optional[flax.core.frozen_dict.FrozenDict[str, Any]]])

@zaxtax
Copy link
Collaborator Author

zaxtax commented Dec 7, 2022

@zaxtax - seeing a small number of new internal pytype bugs of this sort:

File "rax/examples/flax_integration/main.py", line 143, in train_step: Function FrozenDict.copy was called with the wrong arguments [wrong-arg-types]
         Expected: (self, add_or_replace: flax.core.frozen_dict.FrozenDict[str, Mapping[str, Any]])
  Actually passed: (self, add_or_replace: Dict[str, Any])

File "jestimator/states.py", line 73, in variables: Function FrozenDict.copy was called with the wrong arguments [wrong-arg-types]
         Expected: (self, add_or_replace: flax.core.frozen_dict.FrozenDict[str, Any])
  Actually passed: (self, add_or_replace: Dict[str, Optional[flax.core.frozen_dict.FrozenDict[str, Any]]])

File "jestimator/states.py", line 143, in variables: Function FrozenDict.copy was called with the wrong arguments [wrong-arg-types]
         Expected: (self, add_or_replace: flax.core.frozen_dict.FrozenDict[str, Any])
  Actually passed: (self, add_or_replace: Dict[str, Optional[flax.core.frozen_dict.FrozenDict[str, Any]]])

@levskaya try now

@copybara-service copybara-service bot merged commit 5bd4cd2 into main Dec 12, 2022
@copybara-service copybara-service bot deleted the mypy_build branch December 12, 2022 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants