-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Consolidate typing for coords with Coords and StrongCoords aliases (fixes #7972) #7987
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
base: main
Are you sure you want to change the base?
Conversation
pymc/distributions/shape_utils.py
Outdated
| # Lazy import to break circular dependency | ||
| if model is None: | ||
| from pymc.model.core import modelcontext | ||
|
|
||
| model = modelcontext(None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this functional change? Doesn't seem necessary or a good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i introduced the lazy import only to work around the circular import that surfaced during the typing consolidation (shape_utils -> model -> distributions -> shape_utils). I agree that changing runtime behavior here is not ideal
if you prefer, i can revert this functional change and instead resolve the circular dependency purely by relocating the typing aliases to a more independent module
pymc/distributions/shape_utils.py
Outdated
| CoordValue: TypeAlias = Sequence[Hashable] | np.ndarray | None | ||
| Coords: TypeAlias = Mapping[str, CoordValue] | ||
|
|
||
| StrongCoordValue: TypeAlias = tuple[Hashable, ...] | None | ||
| StrongCoords: TypeAlias = Mapping[str, StrongCoordValue] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put these in a more independent place to avoid the new circular import issues. Coordinates are not really distribution specific anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can move CoordValue, Coords, StrongCoordValue, and StrongCoords to a more independent location and update downstream imports accordingly. Let me know if you have a preferred target module for these.
|
@ricardoV94 it would be really helpful if you could review the changes! |
|
I'll ping some folks who are bigger fans of type-hints than me for review. A whole new file seems a bit cluttering, although it could be useful to move more things in the future. My gut feeling would be to just throw this into one of the |
|
@ricardoV94 Thanks for the feedback!! I have implemented the changes!! |
|
Thoughts on a from pymc.typing import Coords
import pymc as pm
coords: Coords = {"covariate": ["A", "B", "C"]}
model = pm.Model(coords=coords)Or directly from Seems out of place in the |
|
What would go in it? If it's just a handful doesn't seem worth it |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7987 +/- ##
=======================================
Coverage 91.49% 91.50%
=======================================
Files 116 116
Lines 18963 18992 +29
=======================================
+ Hits 17350 17378 +28
- Misses 1613 1614 +1
🚀 New features to boost your workflow:
|
|
Thanks for the feedback! @ricardoV94 @williambdean Right now it’s just the coord/shape aliases (Coords, CoordValue, StrongCoords, etc.). Reason for grouping them was: But I'm also happy to keep everything inside |
this PR addresses issue #7972 by consolidating the typing of
coordsacross the PyMC codebase.Previously,
coordsappeared with a mix of types such as:SequenceSequence | np.ndarraydict[str, Any]this PR introduces explicit and consistent type aliases:
CoordValueCoordsStrongCoordValueStrongCoords(similar to the existing
Shape,Dims, andStrongDimsaliases inshape_utils)Key changes
Model.coordsto returnStrongCoordsStrongCoordsinstead ofdict[str, Any}Sequence,Sequence | np.ndarray, and untyped dictionariesNotes
while testing this change, a runtime circular import involving:
pymc.model.corepymc.distributions.shape_utilsfrom pymc.model import modelcontextpymc.model(partially initialized)was encountered.
Fixes #7972