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

fix mypy errors: component.py #549

Merged
merged 6 commits into from
Dec 11, 2024

Conversation

stevebachmeier
Copy link
Contributor

Fix mypy errors: component.py

Description

Changes and notes

Testing

@@ -92,7 +92,24 @@ class Component(ABC):
component. An empty dictionary indicates no managed configurations.
"""

def __repr__(self):
def __init__(self) -> None:
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 is a directly copy/paste that moves the constructor to the top. mypy really didn't like the init being at the bottom of the file.

@stevebachmeier stevebachmeier marked this pull request as draft December 6, 2024 23:58
@@ -791,7 +798,7 @@ def _register_simulant_initializer(self, builder: Builder) -> None:

if type(self).on_initialize_simulants != Component.on_initialize_simulants:
builder.population.initializes_simulants(
self, creates_columns=self.columns_created, **initialization_requirements
self, creates_columns=self.columns_created, **initialization_requirements # type: ignore[arg-type]
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 block is temporary anyway since we're deprecating old functionalty here. It does highlight the fact, however, that we need to figure out how to best type hint kwargs that get unpacked at the signature. the current solution seems complicated. https://typing.readthedocs.io/en/latest/spec/callables.html#unpack-kwargs

else:
try:
data = builder.data.load(data_source)
except ArtifactException:
raise ConfigurationError(
f"Failed to find key '{data_source}' in artifact."
)
elif isinstance(data_source, Callable):
elif callable(data_source):
Copy link
Contributor Author

@stevebachmeier stevebachmeier Dec 7, 2024

Choose a reason for hiding this comment

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

til about the built-in callable and how isinstance(func, collections.abc.Callable) isn't proper

"""
return None
return ""
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a big change based on validation for empty strings vs None. What is the reason for this change or was the type hinting just wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still working on this (the PR is in draft)

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 it's fine, though. It did indeed get flagged by mypy but all of the usage of the query are handling strings

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 guess there's a chance that some downstream code is assuming this could be None. I can handle it if we think that's actually the case. but vph isn't using this property at all and everything in vivarium handles it as a str. (all of the defaults, e.g. are "", not None)

@@ -606,7 +607,9 @@ def build_lookup_table(
raise ConfigurationError(f"Data '{data}' must be a LookupTableData instance.")

if isinstance(data, list):
return builder.lookup.build_table(data, value_columns=list(value_columns))
return builder.lookup.build_table(
data, value_columns=list(value_columns) if value_columns else ()
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this pass an empty list if value columns is None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doesn't really batter b/c build_table takes a Sequence (in fact it defaults to ())

@stevebachmeier stevebachmeier marked this pull request as ready for review December 10, 2024 17:17
all_columns = list(data.columns)
if value_columns is None:
if not self.get_value_columns:
Copy link
Contributor

Choose a reason for hiding this comment

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

self.get_value_columns will never actually be None at this point, since it's being set earlier in setup_component(). I think it's better to ignore the mypy error than introduce this check that will never trigger.

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 can cast?

@@ -38,7 +37,7 @@ def _terminal_logging_not_configured() -> bool:
# fragile since it depends on a loguru's internals as well as the stability of code
# paths in vivarium, but both are quite stable at this point, so I think it's pretty,
# low risk.
return 1 not in logger._core.handlers # type: ignore[attr-defined]
return 1 not in loguru.logger._core.handlers # type: ignore[attr-defined]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this change needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a generic cleanup (we're already importing all of loguru). And to make it consistent w/ te component.py

@albrja albrja self-requested a review December 10, 2024 21:11
@stevebachmeier stevebachmeier merged commit f5ae0b6 into main Dec 11, 2024
7 checks passed
@stevebachmeier stevebachmeier deleted the sbachmei/mic-5599/fix-mypy-errors-component branch December 11, 2024 18:18
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants