Skip to content

Improve mypy coverage by adding --namespace-packages #3049

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

Merged
merged 14 commits into from
Dec 11, 2019

Conversation

chriselion
Copy link
Contributor

Turning on --namespace-packages gets mypy to recognize more types across files. Unfortunately it also triggers an assert in mypy, because it ends up reporting the same file twice (see python/mypy#7510).

I haven't seen any progress on the mypy issue, so I forked, disabled the assertion, and set up precommit to look at the fork instead.

This PR fixes the errors uncovered by doing this.

@@ -494,7 +495,7 @@ def _get_state(self, output: UnityRLOutputProto) -> AllBrainInfo:

@staticmethod
def _parse_side_channel_message(
side_channels: Dict[int, SideChannel], data: bytearray
side_channels: Dict[int, SideChannel], data: bytes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bytes is immutable - these seemed better for processing inputs. I left outputs as bytearrays though.

agent_info_list: List[AgentInfoProto],
agent_info_list: Collection[
AgentInfoProto
], # pylint: disable=unsubscriptable-object
Copy link
Contributor Author

Choose a reason for hiding this comment

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

return self._float_properties.keys()
return list(self._float_properties.keys())

def get_property_dict(self) -> Dict[str, 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.

Add this since the usage pattern was always list_properties then get_property on each.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need get and list properties then?
Also, it is not clear that modifying this dict will not modify the unity environment...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure we need list. I think get() is still useful.

Would get_property_dict_copy() make it clearer? Unfortunately there's no clean way to return a read-only dictionary.

@@ -28,7 +28,7 @@ def reset_local_buffers(self) -> None:
def append_to_update_buffer(
self,
update_buffer: AgentBuffer,
agent_id: str,
agent_id: int,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ervteng Is this correct now? mypy was complaining about demo_loader calling this with 0 instead of a string.

Copy link
Contributor

Choose a reason for hiding this comment

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

Demo loader is wrong, it uses an int for the update buffer. However in env_manager that int is turned into a string, so the int won't work in the general case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually it's both, in different parts of the code :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made it a Union[int, str] for now

return self._float_properties.keys()
return list(self._float_properties.keys())

def get_property_dict(self) -> Dict[str, float]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need get and list properties then?
Also, it is not clear that modifying this dict will not modify the unity environment...

self.all_log_probs: Optional[tf.Tensor] = None
self.output: Optional[tf.Tensor] = None
self.selected_actions: Optional[tf.Tensor] = None
self.action_holder: Optional[tf.Tensor] = None
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you intend to add those? It does not seem related to mypy coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, there were places where mypy was complaining (correctly) that e.g. all_log_probs wasn't a member of LearningModel. In practice it was OK because they were always defined in PPOModel or SACModel. This just defines them in the base class too.

import numpy as np
from enum import Enum

AgentId = NewType("AgentId", int)
AgentGroup = NewType("AgentGroup", str)
AgentId = int
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 this was going to be more trouble that it was worth. This still aliases the types so that it's a little clearer than the raw int/str types.

@@ -371,8 +372,8 @@ def set_action_for_agent(
action = action.astype(expected_type)

if agent_group not in self._env_actions:
self._env_actions[agent_group] = self._empty_action(
spec, self._env_state[agent_group].n_agents()
self._env_actions[agent_group] = spec.create_empty_action(
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 was a bug.

Copy link
Contributor

@ervteng ervteng left a comment

Choose a reason for hiding this comment

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

Looks good from my side

@chriselion chriselion changed the title Improve mypy coverage Improve mypy coverage by adding --namespace-packages Dec 11, 2019
@chriselion chriselion merged commit 0d4836d into master Dec 11, 2019
@delete-merged-branch delete-merged-branch bot deleted the develop-mypy-coverage branch December 11, 2019 23:47
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants