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

Sbachmei/mic 5549/mypy results context #538

Merged
merged 6 commits into from
Nov 13, 2024

Conversation

stevebachmeier
Copy link
Contributor

Fix mypy errors in framework/results/context.py

Description

Changes and notes

Testing

@@ -356,7 +356,7 @@ def _get_groups(
)
else:
pop_groups = filtered_pop.groupby(lambda _: "all")
return pop_groups
return pop_groups # type: ignore[return-value]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to talk about this. I think it's a discrepancy between mypy requiring DataFrameGroupBys to be generic and python RuntimeErroring w/ DataFrameGroupBy is not subscriptable. from __future__ import annotations does not fix like it does for pd.Series.

@@ -301,7 +302,7 @@ def _bin_data(data: Union[pd.Series, pd.DataFrame]) -> pd.Series:

def register_observation(
self,
observation_type,
observation_type: Type[BaseObservation],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This wasn't causing an error but it was just wrong. At this point we're sending in the class itself, NOT an instantiation of it (actually, it might be worth just saying observation_type: Type[StratifiedObservation | UnstratifiedObservation | AddingObservation | ConcatenatingObservation] if folks prefer b/c that's what it really is. But they all inherit from BaseObservation so this is less typing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should BaseObservation be called Observation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we had it called that at once and changed it to this but now can't recall why. It does subclass from abc, though there are no abstract methods or anything on it. Probably doesn't matter?

Copy link
Contributor

@patricktnast patricktnast Nov 13, 2024

Choose a reason for hiding this comment

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

Type[C] is covariant, so I think putting in the base observation makes sense. It looks like there is a builtin type you could use instead, though

@@ -301,7 +302,7 @@ def _bin_data(data: Union[pd.Series, pd.DataFrame]) -> pd.Series:

def register_observation(
self,
observation_type,
observation_type: Type[BaseObservation],
Copy link
Contributor

Choose a reason for hiding this comment

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

Should BaseObservation be called Observation?

@@ -60,7 +59,8 @@ class BaseObservation(ABC):
DataFrame or one with a complete set of stratifications as the index and
all values set to 0.0."""
results_gatherer: Callable[
[pd.DataFrame | DataFrameGroupBy[str], tuple[str, ...] | None], pd.DataFrame
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming the things in this file were just type-hinted incorrectly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, though I'm not gonna lie - I'm having trouble understanding how to type-hint DataFrameGroupBys...

@stevebachmeier stevebachmeier merged commit 881d6b8 into main Nov 13, 2024
6 checks passed
@stevebachmeier stevebachmeier deleted the sbachmei/mic-5549/mypy-results-context branch November 13, 2024 18:30
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.

3 participants