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

Split expected emission factors into direct and lifecycle #5420

Merged
merged 6 commits into from
May 30, 2023

Conversation

qyearsley
Copy link
Contributor

Issue

Related to #3990

Description

Splits the expected range values into direct and lifecycle.

Note that in the case of test failure, the output does have information whether the lifecycle or direct test case failed, although it may not be obvious from the message.

Double check

  • I have tested my parser changes locally with poetry run test_parser "zone_key"
  • [] I have run pnpx prettier --write . and poetry run format to format my changes.

@github-actions github-actions bot added the tests label May 20, 2023
Copy link
Member

@VIKTORVAV99 VIKTORVAV99 left a comment

Choose a reason for hiding this comment

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

The tests work as expected but we need to take a look at the types in the test itself, there are a lot of type errors in here.

I also noted that we still rely on the .ts file to get the possible modes. We should probably update this whole test to use the constants from the python parts instead so we decuple this from web.

The python constants are located at electricitymap/contrib/config/constants

The callback is called with the mode and factors for both the defaults
and the zone-specific overrides; each `factors` object is either a list
of factor dicts or a single factor dict, and each factor dict has the
key "value", and possibly other keys such as "source".
"""
emission_factors = cls.parameters["emissionFactors"]
Copy link
Member

Choose a reason for hiding this comment

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

Pylance is reporting that parameters is unknown on the type Type[CO2eqParametersDirectAndLifecycleMixin] so we have to take care of that if we want to add any form of type checking in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that many of the warnings were missing assert methods since the mixin class was not a subclass of TestCase.

One of the simplest ways to deal with this was to make it a subclass of TestCase; after doing that however it is found automatically by the test runner so to prevent unexpected behavior, in the latest revision I nested it inside of another class, like in https://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class

tests/test_co2eq_parameters.py Outdated Show resolved Hide resolved
@@ -384,6 +383,28 @@ class CO2eqParametersDirect(CO2eqParametersDirectAndLifecycleMixin, unittest.Tes

parameters = CO2EQ_PARAMETERS_DIRECT

# Expected min and max values for emission factors, by mode.
ranges_by_mode = {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ranges_by_mode = {
ranges_by_mode: Dict[str, Tuple[Union[int, float], Union[int, float]]] = {

You might have to update the imports from typing after adding this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done; note that I discovered that type float sort of includes int; and there's also type numbers.Number which is already imported that includes float, int and other number types.

https://stackoverflow.com/questions/50928592/mypy-type-hint-unionfloat-int-is-there-a-number-type

@@ -392,6 +413,28 @@ class CO2eqParametersLifecycle(

parameters = CO2EQ_PARAMETERS_LIFECYCLE

# Expected min and max values for emission factors, by mode.
ranges_by_mode = {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ranges_by_mode = {
ranges_by_mode: Dict[str, Tuple[Union[int, float], Union[int, float]]] = {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@qyearsley
Copy link
Contributor Author

This doesn't yet resolve 100% of the issues flagged by pyright/pylance -- I propose resolving more of these issues in a separate PR.

For some reason numbers.Numbers does not play well with literal tuples
so I changed it to a int and float union which work.
Copy link
Member

@VIKTORVAV99 VIKTORVAV99 left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@VIKTORVAV99 VIKTORVAV99 enabled auto-merge (squash) May 30, 2023 13:18
@VIKTORVAV99 VIKTORVAV99 merged commit ef89565 into electricitymaps:master May 30, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants