Skip to content

Add mypy type checking; fix resultant errors.#85

Open
sunash wants to merge 47 commits into
masterfrom
dev
Open

Add mypy type checking; fix resultant errors.#85
sunash wants to merge 47 commits into
masterfrom
dev

Conversation

@sunash
Copy link
Copy Markdown
Collaborator

@sunash sunash commented Nov 5, 2020

This PR also includes functionality to allow for altering an EVSE after constraints have been added.

sunash added 27 commits October 13, 2020 12:55
register_evse now edits _voltage and _phase_angles elements if an EVSE is re-registered instead of appending.
I.e. by using add_constraint instead of setting directly.
This avoids a mypy error regarding setting object attributes via accessors.
Fix type of algo in TestSortedAlgorithms to Optional[SortedSchedulingAlgo].
Remove redundant type definitons.
Check that algo is not None before running tests, raising informative error if this is not the case.
This satisfies mypy's type checker.
This allows for tests to set the estimator without triggering a type checker error.
The objects being del'd are not defined anyway.
This keeps with LSP (consistency with Interface.is_feasible).
…laced

with new names in case types actually do change.
Change signature of _to/_from_dict to allow for optional context or loaded dicts.
Type session_generator output to TypedDict so mypy can do more specific checks.
Add typing_extensions to setup requirements.
Conditionally import forward references using TYPE_CHECKING.
Add error to constraint_currents if no constraints have been added.
SortFuncCallback allows for SortedSchedulingAlgo._sort_fn to be
set with a Callable without mypy thinking it's a bound method.
Allow SessionInfo constructor to take ndarrays as inputs for min/max_rates.
Edit sessions types in test_preprocessing.py to reflect if it's SessionDict or SessionInfo.
Remove del statements for type checker to pass (Discuss).
Add checks for float and int in scalar max/min rates checks.
Add comments for type checker to ignore object returns from pydoc.locate and _build_from_id.
# Conflicts:
#	acnportal/acnsim/network/tests/test_network.py
@sunash sunash marked this pull request as ready for review November 11, 2020 16:23
Copy link
Copy Markdown
Collaborator Author

@sunash sunash left a comment

Choose a reason for hiding this comment

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

Just some explanatory comments and TODOs for myself (@sunash)

Comment thread acnportal/acnsim/network/charging_network.py
# If there are no constraints, throw an error as this function is not well-
# defined.
# TODO: Test the no constraint case for is_feasible and constraint_currents.
if self.constraint_matrix is None:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@zach401 Does this look good for the None case? If so I'll write a test

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think we should try to support the no constraint case. I thought this was already covered, but maybe not?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

is_feasible returns True if called when no constraints are added. It's constraint_current that throws an error if called without constraints since there aren't any currents to constrain. But we could also just return an empty NumPy array. I think that's our best bet if we want to avoid throwing an error. I think not erroring, in this case, is fine as long as we're clear about the behavior in the docstring. @zach401 sound good?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

To clarify, I'd return np.array([]) if self.constraint_matrix is None (and add a test).

Comment thread acnportal/acnsim/simulator.py Outdated
self.max_recompute = scheduler.max_recompute
self.scheduler.register_interface(interface_type(self))

def generate_interface(self, interface_type: type) -> Interface:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@zach401 For cases when an input scheduler is not given to the Simulator, we still need a way to get an interface to a simulator (for, e.g. RL training in which a simulation is stepped). If this looks good I'll write a test

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What do you think about get_interface() instead?
I also don't understand the doc string, why does it matter if scheduler is None? This function doesn't case what the scheduler is, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I had anticipated that it would only ever be called when scheduler is None because otherwise, the Interface is accessible through scheduler.interface. That being said, the method itself doesn't require that, so I'll update the docstring and change to get_interface.

if not isinstance(loaded_event, Event):
warnings.warn(
f"Got an element of type {loaded_event} when loading ev_history,"
f"which is not an instance of EV or a subclass. Loaded "
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

TODO @sunash needs to say "...instance of Event or a subclass"

if self.max_rate_estimator is not None:
self.max_rate_estimator.register_interface(interface)

def register_max_rate_estimator(self, interface: Interface) -> None:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

TODO @sunash This needs to be implemented, right now it's a register_interface clone

Copy link
Copy Markdown
Owner

@zach401 zach401 left a comment

Choose a reason for hiding this comment

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

See replies above.

Comment thread acnportal/acnsim/network/charging_network.py
# If there are no constraints, throw an error as this function is not well-
# defined.
# TODO: Test the no constraint case for is_feasible and constraint_currents.
if self.constraint_matrix is None:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think we should try to support the no constraint case. I thought this was already covered, but maybe not?

Comment thread acnportal/acnsim/simulator.py Outdated
self.max_recompute = scheduler.max_recompute
self.scheduler.register_interface(interface_type(self))

def generate_interface(self, interface_type: type) -> Interface:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What do you think about get_interface() instead?
I also don't understand the doc string, why does it matter if scheduler is None? This function doesn't case what the scheduler is, right?

# Conflicts:
#	.travis.yml
#	acnportal/acnsim/network/tests/test_network.py
#	setup.py
Ignore one error with battery_kwargs.
# Conflicts:
#	.travis.yml
#	acnportal/acnsim/events/event.py
#	acnportal/acnsim/simulator.py
#	acnportal/algorithms/tests/generate_test_cases.py
#	tutorials/lesson2_implementing_a_custom_algorithm.ipynb
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@chrisyeh96
Copy link
Copy Markdown
Collaborator

@sunash @zach401 What's the status on this pull request? It would be great to have more Python typing hints for us to develop our gym against

@sunash
Copy link
Copy Markdown
Collaborator Author

sunash commented Jul 20, 2022

We would need to get all the type hinting to pass and address the remaining comments. Also if resolving #106 yields a minimum version of python that has future.annotations, the type hints might use that package rather than typing. Either way we should address #106 before moving forward with this PR.

livctr and others added 13 commits September 14, 2023 15:16
Documents build/test commands, high-level architecture of acnsim
(Simulator, Interface, ChargingNetwork, BaseAlgorithm, BaseSimObj
serialization), and key style conventions from CONTRIBUTING.md.

https://claude.ai/code/session_01FpQTLzRzzeVEdGU5LVLDps
It randomizes EV-to-station assignment (with a waiting queue), not
constraint limit values.

https://claude.ai/code/session_01FpQTLzRzzeVEdGU5LVLDps
- Remove incorrect "three main packages" count (there are five)
- Replace # noqa (never used) with the actual # noinspection PyProtectedMember annotation

https://claude.ai/code/session_01FpQTLzRzzeVEdGU5LVLDps
The EV/Battery/BaseEVSE bullets restated what class names and file paths
already communicate. Per best practice, only include what Claude can't
infer from reading code.

https://claude.ai/code/session_01FpQTLzRzzeVEdGU5LVLDps
Resolves conflicts in type hints (Optional return types, Dict vs dict
for Python 3.8 compatibility) and docstring formatting in favor of
master's intentional improvements.
Sync dev with master (v0.3.3 + pandas/Python version updates)
…J4Ag5

Add comprehensive CLAUDE.md documentation
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.

5 participants