Skip to content
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

Enable mypy and isort in pre-commit & CI #1346

Merged
merged 5 commits into from Nov 28, 2022
Merged

Enable mypy and isort in pre-commit & CI #1346

merged 5 commits into from Nov 28, 2022

Conversation

ml-evs
Copy link
Member

@ml-evs ml-evs commented Oct 2, 2022

This PR fixes many broken/dodgy type hints in the code, and a few associated bugs/undefined behaviours.

  • Enable mypy in CI
  • Enable and apply isort
  • Remove simplejson (not really needed since we dropped 3.7 support)
  • Update pre-commit
  • Add linters and fixers to dev requirements

@codecov
Copy link

codecov bot commented Oct 2, 2022

Codecov Report

Merging #1346 (01504c9) into master (62b83fb) will decrease coverage by 0.08%.
The diff coverage is 95.44%.

❗ Current head 01504c9 differs from pull request most recent head 91acd47. Consider uploading reports for the commit 91acd47 to get more accurate results

@@            Coverage Diff             @@
##           master    #1346      +/-   ##
==========================================
- Coverage   91.42%   91.33%   -0.09%     
==========================================
  Files          72       72              
  Lines        4372     4363       -9     
==========================================
- Hits         3997     3985      -12     
- Misses        375      378       +3     
Flag Coverage Δ
project 91.33% <95.44%> (-0.09%) ⬇️
validator 91.43% <98.02%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
optimade/adapters/references/__init__.py 100.00% <ø> (ø)
optimade/adapters/structures/__init__.py 100.00% <ø> (ø)
optimade/server/mappers/__init__.py 100.00% <ø> (ø)
optimade/server/routers/references.py 100.00% <ø> (ø)
optimade/utils.py 58.20% <44.44%> (-3.56%) ⬇️
optimade/adapters/structures/utils.py 83.84% <64.28%> (ø)
optimade/filtertransformers/elasticsearch.py 85.94% <83.33%> (ø)
optimade/client/client.py 79.01% <87.50%> (-0.26%) ⬇️
optimade/server/middleware.py 94.70% <95.23%> (-0.17%) ⬇️
optimade/validator/validator.py 83.45% <95.23%> (-0.09%) ⬇️
... and 53 more

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

@JPBergsma
Copy link
Contributor

In principle, this is a good idea.
I however think that we should solve all the warnings mypy gives (or silence them with # type: ignore if they are false alarms) before we merge this.
Now it gives hundreds of warnings, I won't notice if my PR will add one extra warning, so I do not think it would be very useful at this point.

@ml-evs ml-evs changed the title Enable MyPy in pre-commit & CI Enable MyPy and isort in pre-commit & CI Oct 25, 2022
@ml-evs ml-evs force-pushed the ml-evs/enable_mypy branch 3 times, most recently from cbd91b0 to 49851eb Compare November 11, 2022 19:37
@ml-evs ml-evs marked this pull request as ready for review November 11, 2022 21:11
@ml-evs
Copy link
Member Author

ml-evs commented Nov 11, 2022

Sorry for the kinda monstrous PR @JPBergsma, I think I will split it into two PRs with isort and then mypy. I didn't have to change any tests from my mypy changes so hopefully it should all just work...

@JPBergsma
Copy link
Contributor

For me, it is not necessary to split in two. I think both changes are quite orthogonal, so I can review both of them at the same time.

@ml-evs
Copy link
Member Author

ml-evs commented Nov 14, 2022

For me, it is not necessary to split in two. I think both changes are quite orthogonal, so I can review both of them at the same time.

Okay great, I guess my suggestion is to ignore the changes from 4782f5e and just check mypy first. The isort changes do not really need reviewing.

Once you are happy with it I will rebase this PR into a few commits so that we have the separation going forward.

@ml-evs
Copy link
Member Author

ml-evs commented Nov 14, 2022

(We can also merge your other PRs before this one!)

@JPBergsma
Copy link
Contributor

JPBergsma commented Nov 14, 2022

I am still having some problems with PR#1304.

The docker image setup seems to fail because of yaml missing.
which is strange as this PR did not change anything about the dependancies.

And the tests still fail for the elastic search backend.
So now I am trying to set up elastic locally to see why the tests are failing.

I tried to set up the docker container according to the installation instructions, but then I get an error that optimade can't connect to elastic search: "ERROR tests/server/middleware/test_api_hint.py - elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f4e5cdb62b0>: Failed to establish a new connection: [Errno 111] Connection ..."

So I guess I still have some more work to do before my pr is merge ready.

ps. after executing docker network create elastic the tests do start, but I still receive many errors.

@ml-evs
Copy link
Member Author

ml-evs commented Nov 14, 2022

I am still having some problems with PR#1304.

The docker image setup seems to fail because of yaml missing. which is strange as this PR did not change anything about the dependancies.

And the tests still fail for the elastic search backend. So now I am trying to set up elastic locally to see why the tests are failing.

I tried to set up the docker container according to the installation instructions, but then I get an error that optimade can't connect to elastic search: "ERROR tests/server/middleware/test_api_hint.py - elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f4e5cdb62b0>: Failed to establish a new connection: [Errno 111] Connection ..."

So I guess I still have some more work to do before my pr is merge ready.

ps. after executing docker network create elastic the tests do start, but I still receive many errors.

I also ran into this (#1377), not sure if the docker oneliner was tested before we upgrade to ES v7. Will try to address this in #1373

requirements-dev.txt Outdated Show resolved Hide resolved
@ml-evs
Copy link
Member Author

ml-evs commented Nov 18, 2022

Just had to do a bit of a messy rebase after we merged that last PR, hope that doesn't mess up your review, but at least now the commits should each contain logical diffs

@JPBergsma
Copy link
Contributor

When I run mypy on my machine, I still get two errors.

  1. optimade/adapters/structures/utils.py:223: error: Need type annotation for "Y" [var-annotated]
    Replacing line 223 with Y: np.ndarray = np.cross(Z, X) should solve this warning.

  2. optimade/server/exception_handlers.py:231: error: List item 3 has incompatible type "Tuple[Type[VisitError], Callable[[Any, VisitError], JSONAPIResponse]]"; expected "Tuple[Type[Exception], Callable[[Any, Exception], JSONAPIResponse]]" [list-item]

    I think black caused this line to be broken up so you need a second: # type: ignore[list-item] # not entirely sure why this entry triggers mypy

- Fix all implicit optional type hints (PEP484)
- Replaces all `a: int = None` with `a: Optional[int] = None`
- Automated with `pipx run no_implicit_optional <path>`
- Add many mypy ignores
- Fix importlib shenanigans
@ml-evs ml-evs force-pushed the ml-evs/enable_mypy branch 3 times, most recently from d821885 to e92e1a5 Compare November 24, 2022 20:47
@ml-evs
Copy link
Member Author

ml-evs commented Nov 26, 2022

Thanks for the really thorough review @JPBergsma -- I think for the sake of our time, we can consider this PR ready... unless there's any objections I'll merge later today.

@JPBergsma
Copy link
Contributor

If you want, you can merge it. I still have not finished reviewing all the files yet, so I may still suggest some changes in a later PR.

@ml-evs
Copy link
Member Author

ml-evs commented Nov 26, 2022

If you want, you can merge it. I still have not finished reviewing all the files yet, so I may still suggest some changes in a later PR.

Okay, happy to hold off if you want to keep going.

@ml-evs
Copy link
Member Author

ml-evs commented Nov 27, 2022

I've just tried adding the --warn_unused_ignores config option to mypy and it doesn't even seem self-consistent. When I remove the ignores in the base adapters (after it says they are not needed), the same invocation then fails. Is this the same way you have been finding which ignores are not needed?

@JPBergsma
Copy link
Contributor

I mostly try to understand why the ignore statement is needed.
If it is not clear, I remove it to see what kind of error mypy gives.
Sometimes I do not get an error, and in that case I make a remark here that the ignore statement is not needed.

tests/server/utils.py Outdated Show resolved Hide resolved
@JPBergsma
Copy link
Contributor

I have finished the review. So there should not be any new comments.

Co-authored-by: Johan Bergsma <JPBergsma@users.noreply.github.com>
@ml-evs
Copy link
Member Author

ml-evs commented Nov 28, 2022

Thanks @JPBergsma. I've addressed your final comments and will merge once the tests pass.

@ml-evs ml-evs changed the title Enable MyPy and isort in pre-commit & CI Enable mypy and isort in pre-commit & CI Nov 28, 2022
message = msg.split("\n")
if validator.verbosity > 1:
# ValidationErrors from pydantic already include very detailed errors
# that get duplicated in the traceback
if not isinstance(result, ValidationError):
message += traceback.split("\n")

message = "\n".join(message)

failure_type = None
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not so happy with this line. The value of failure_type will be overwritten later on, so this assignment looks rather strange.
Is it such a problem to define the type on what is now line 377?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a problem, but not of any importance, I don't think. I'd rather have the default value outside of the clause rather than a type-hint inside the clause. Not everything has to be explicitly typed (and we shouldn't let mypy dictate how we code beyond any real errors, especially as it changes between versions).

Copy link
Contributor

@JPBergsma JPBergsma left a comment

Choose a reason for hiding this comment

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

LGTM

@ml-evs ml-evs merged commit b9185cc into master Nov 28, 2022
@ml-evs ml-evs deleted the ml-evs/enable_mypy branch November 28, 2022 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants