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

Typing: Add typehinting for Rod modules #341

Merged
merged 3 commits into from
May 20, 2024

Conversation

ankith26
Copy link

@ankith26 ankith26 commented Mar 9, 2024

This PR does some initial work on #255

This is a WIP, and I've opened this PR to get some feedback, before I go ahead and look into more modules.

@skim0119 skim0119 added the enhancement New feature or request label Mar 9, 2024
@skim0119
Copy link
Collaborator

skim0119 commented Mar 9, 2024

@ankith26 I allowed the github-action to run for you to check. For formatting, you can use make formatting or setup-up a pre-commit hook.

@skim0119 skim0119 changed the base branch from master to update-0.3.3 March 26, 2024 20:27
@skim0119
Copy link
Collaborator

@armantekinalp Since we are bumping up to 3.10, lets see if we can pass this PR and add mypy checker.

@armantekinalp
Copy link
Contributor

@ankith26 if you are still interested in this issue can you resolve the conflicts please.

Copy link

codecov bot commented Apr 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (update/mypy@4d05804). Learn more about missing BASE report.

Current head 4ec414a differs from pull request most recent head 856e901

Please upload reports for the commit 856e901 to get more accurate results.

Additional details and impacted files
@@              Coverage Diff               @@
##             update/mypy     #341   +/-   ##
==============================================
  Coverage               ?   87.80%           
==============================================
  Files                  ?       43           
  Lines                  ?     2943           
  Branches               ?      341           
==============================================
  Hits                   ?     2584           
  Misses                 ?      335           
  Partials               ?       24           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ankith26
Copy link
Author

I have force pushed with the conflicts fixed. The conflicts were mainly due to the fact that I was adding type hints to the functions that were being deprecated in the update-0.3.3 branch. I removed my changes in these cases

@armantekinalp
Copy link
Contributor

@ankith26 thanks. Can you also see why the CI builds fail?

@ankith26
Copy link
Author

While resolving the conflicts I forgot to remove an import that is now unneeded. I have removed that import to fix the fail, and also made sure make autoflake8-check passed locally

@skim0119 skim0119 changed the base branch from update-0.3.3 to update/mypy April 28, 2024 18:07
@skim0119 skim0119 mentioned this pull request Apr 28, 2024
17 tasks
@skim0119
Copy link
Collaborator

@ankith26 Hi -- I added mypy on GitHub action, and, as we have somewhat expected, it is becoming a large job to finish. We have decided that we should split the work on #255, and we can probably pass this PR without completely checking out all CIs.

Thank you again for your contribution. If you can finish typehinting for elastica/rod directory, we'll merge this PR with #367. You can run mypy --config-file pyproject.toml elastica/rod

@skim0119 skim0119 changed the title Add some more typehinting to files at project root Add typehinting for Rod modules Apr 28, 2024
@ankith26
Copy link
Author

If you can finish typehinting for elastica/rod directory, we'll merge this PR with #367.

Sure I'd like to work on this this

@armantekinalp armantekinalp changed the title Add typehinting for Rod modules Typing: Add typehinting for Rod modules Apr 30, 2024
@ankith26
Copy link
Author

ankith26 commented May 6, 2024

Update: I couldn't do much on this PR in the past week due to being busy with other commitments, but I plan to get this PR done from my side in the coming week.

@armantekinalp
Copy link
Contributor

@ankith26 no worries, also please see the other open PRs related to mypy. That could be useful for you.

@ankith26 ankith26 force-pushed the more-typing branch 2 times, most recently from 7afa435 to 53db1bc Compare May 7, 2024 18:00
@ankith26
Copy link
Author

ankith26 commented May 9, 2024

A couple of notes

  • mypy considers np.floating and builtin float incompatible. This is causing trouble in a couple of places.
  • mypy also doesn't like the usage of numba decorators, as they apparently don't preserve typing info.

Any suggestions on how to deal with above issues?

@ankith26
Copy link
Author

I have implemented a workaround for the float issue. In elastica.typing I have defined Float: TypeAlias = Union[np.floating, float], and now this new type can be used where a single float is expected

@skim0119
Copy link
Collaborator

skim0119 commented May 11, 2024

I have implemented a workaround for the float issue. In elastica.typing I have defined Float: TypeAlias = Union[np.floating, float], and now this new type can be used where a single float is expected

In this PR, please just use np.floating whenever you need to type float type for now, and don't create another type alias. We'll make that decision later.

mypy also doesn't like the usage of numba decorators, as they apparently don't preserve typing info.

The function should be typed as if they are just python/numpy function. I understand that
numba decorator will change the function call so mypy would raise error. For now, just ignore them.

@ankith26
Copy link
Author

I have done some more incremental progress, and have removed the Float type as you asked.

@ankith26 ankith26 force-pushed the more-typing branch 2 times, most recently from 89bfbc4 to ebf3a61 Compare May 15, 2024 14:28
@armantekinalp armantekinalp marked this pull request as draft May 15, 2024 18:52
@armantekinalp
Copy link
Contributor

Lets keep in draft until it is ready. Since it burns CI hours for every new commit

@ankith26
Copy link
Author

This PR is nearing completion. Though there are a couple of issues that I don't know how to resolve.

  1. The following attributes are expected in a SystemType instance in various places across the codebase, but not typed into SystemType. How should these be dealt with?
    periodic_boundary_nodes_idx
    periodic_boundary_elems_idx
    director_collection
    radius
    mass
    tangents
    director_collection
    velocity_collection
    internal_forces
    external_forces
    internal_torques
    origin
    normal
    lengths
    length
    ring_rod_flag
    n_elems

  2. Similarly, RodType is used in a couple of places where these attributes are expected:
    velocity_collection
    dilatation
    n_elems

  3. RodType incompatible with SystemType error, what is the actual relation between the two and how can this be fixed in typing?

  4. ConstraintBase: Why are properties constrained_position_idx and constrained_director_idx typed with optional?

  5. How to type a MemoryBlock in cosserat_rot.py

  6. KnotTheoryCompatibleProtocol defines director_collection and radius read-only, but value set in cosserat_rot.py

  7. CosseratRod instance expects the following attributes, but these aren't set in the constructor:
    ghost_elems_idx
    ghost_voronoi_idx

@skim0119
Copy link
Collaborator

Thank you for your hard work. There are some inconsistency because the original code was only developed for Rod implementation, and later we decided to add other types like RigidBody and Surfaces. I'll take care of organizing properties in other PR.

To answer your question:

  1. The following attributes are expected in a SystemType instance in various places across the codebase, but not typed into SystemType. How should these be dealt with?

SystemType should include all Rod, RigidBody, and Surface, so please use it whenever these objects needs to be passed or returned. For now, don't worry about any mypy error caused by missing property.

  1. Similarly, RodType is used in a couple of places where these attributes are expected:

Same as above. We'll have SystemType which is union of RodType, RigidBodyType, and SurfaceType. If you need to type-hint Rod specific module, use RodType.

  1. RodType incompatible with SystemType error, what is the actual relation between the two and how can this be fixed in typing?

Don't worry about it for now.

  1. ConstraintBase: Why are properties constrained_position_idx and constrained_director_idx typed with optional?

There are some boundary condition implementations that do not use _idx in the definition. You don't need to modify at this point.

  1. How to type a MemoryBlock in cosserat_rot.py

Use RodType.

  1. KnotTheoryCompatibleProtocol defines director_collection and radius read-only, but value set in cosserat_rot.py

The protocol for KnotTheory shouldn't write to those properties. If you see any error, I would assume it is because RodType is not fully defined yet.

  1. CosseratRod instance expects the following attributes, but these aren't set in the constructor:

You don't need to worry about it, it will be included in RodType in the later commit.

@ankith26
Copy link
Author

This PR is now done from my side, it is ready for review.

@skim0119 skim0119 marked this pull request as ready for review May 20, 2024 03:40
elastica/rod/knot_theory.py Outdated Show resolved Hide resolved
@skim0119
Copy link
Collaborator

Looks good. One minor change, and we can probably merge.

@ankith26
Copy link
Author

I had added that bit to fix some mypy issues, but didn't realise it broke the unit tests. I have reverted that change, as suggested.

@skim0119 skim0119 merged commit 03c00f4 into GazzolaLab:update/mypy May 20, 2024
2 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants