-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Sbachmei/mic 5549/mypy results context #538
Changes from 2 commits
630b06d
f015a56
e440eb8
e418421
8ce099f
3b80c24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,13 @@ | |
|
||
from collections import defaultdict | ||
from enum import Enum | ||
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union | ||
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Type, Union | ||
|
||
import pandas as pd | ||
|
||
from vivarium.framework.event import Event | ||
from vivarium.framework.results.context import ResultsContext | ||
from vivarium.framework.results.observation import BaseObservation | ||
from vivarium.framework.values import Pipeline | ||
from vivarium.manager import Manager | ||
from vivarium.types import ScalarValue | ||
|
@@ -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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
is_stratified: bool, | ||
name: str, | ||
pop_filter: str, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
from abc import ABC | ||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
from typing import Any | ||
from typing import TYPE_CHECKING | ||
|
||
import pandas as pd | ||
from pandas.api.types import CategoricalDtype | ||
|
@@ -35,6 +35,9 @@ | |
|
||
VALUE_COLUMN = "value" | ||
|
||
if TYPE_CHECKING: | ||
_PandasGroup = pd.DataFrame | DataFrameGroupBy[tuple[str, ...] | str] | ||
|
||
|
||
@dataclass | ||
class BaseObservation(ABC): | ||
|
@@ -60,7 +63,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... |
||
[_PandasGroup, tuple[str, ...] | None], | ||
pd.DataFrame, | ||
] | ||
"""Method or function that gathers the new observation results.""" | ||
results_updater: Callable[[pd.DataFrame, pd.DataFrame], pd.DataFrame] | ||
|
@@ -76,7 +80,7 @@ class BaseObservation(ABC): | |
def observe( | ||
self, | ||
event: Event, | ||
df: pd.DataFrame | DataFrameGroupBy[str], | ||
df: _PandasGroup, | ||
stratifications: tuple[str, ...] | None, | ||
) -> pd.DataFrame | None: | ||
"""Determine whether to observe the given event, and if so, gather the results. | ||
|
@@ -139,7 +143,8 @@ def __init__( | |
to_observe: Callable[[Event], bool] = lambda event: True, | ||
): | ||
def _wrap_results_gatherer( | ||
df: pd.DataFrame | DataFrameGroupBy[str], _: tuple[str, ...] | None | ||
df: _PandasGroup, | ||
_: tuple[str, ...] | None, | ||
) -> pd.DataFrame: | ||
if isinstance(df, DataFrameGroupBy): | ||
raise TypeError( | ||
|
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.
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.