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

Switch to mypy #247

Merged
merged 20 commits into from
Sep 28, 2020
Merged

Switch to mypy #247

merged 20 commits into from
Sep 28, 2020

Conversation

m-vdb
Copy link
Collaborator

@m-vdb m-vdb commented Aug 21, 2020

Proposed changes:

Status (please check what you already did):

  • made PR ready for code review
  • added some tests for the functionality
  • updated the documentation
  • updated the changelog (please check changelog for instructions)
  • reformat files using black (please check Readme for instructions)

@m-vdb
Copy link
Collaborator Author

m-vdb commented Aug 21, 2020

Current status:

  • lots of wrong type annotations due to optional values, that the code sometimes doesn't consider optional
  • sometimes a function is a coroutine, sometimes not, and the typechecker doesn't understand it

(also ran it on rasa, and got "Found 812 errors in 136 files (checked 227 source files)". Ran super fast in a few seconds....)

cc @tmbo @wochinge FYI. There might be some thing we'd want to fix before 2.0 final release, but maybe non-blocking

forms: List[Union[Text, Dict]]


class ActionCall(TypedDict):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

these type definitions need proper review + docstrings

setup.cfg Show resolved Hide resolved
@@ -213,17 +214,18 @@ def entity_is_desired(
"""

# slot name is equal to the entity type
other_slot_equals_entity = other_slot == other_slot_mapping.get("entity")
entity_type = other_slot_mapping.get("entity")
other_slot_equals_entity = other_slot == entity_type
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is part of a small refactoring done in 4dec28a

@@ -567,7 +578,10 @@ async def _validate_if_required(
if utils.is_coroutine_action(self.validate):
return await self.validate(dispatcher, tracker, domain)
else:
return self.validate(dispatcher, tracker, domain)
# see https://github.com/python/mypy/issues/5206
Copy link
Collaborator Author

@m-vdb m-vdb Aug 26, 2020

Choose a reason for hiding this comment

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

if we have to do this a lot in rasa, we might consider contributing to mypy to support useful type guards (I honestly don't know how hard it would be, but it could be worth it)

Copy link
Member

Choose a reason for hiding this comment

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

yes this sounds like a good idea 👍 maybe something for someone from the team to contribute?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes definitely. I will keep this as a note for now, and if this comes back I'll mention it (in backend eng meeting for instance)

Copy link
Member

@tmbo tmbo left a comment

Choose a reason for hiding this comment

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

looks great, excited to replace pytype 💀 😂

rasa_sdk/knowledge_base/actions.py Show resolved Hide resolved
rasa_sdk/types.py Show resolved Hide resolved
from typing_extensions import TypedDict


class TrackerState(TypedDict):
Copy link
Member

Choose a reason for hiding this comment

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

should we rather use pythons new data classes? (and a backport for 3.6 https://pypi.org/project/dataclasses/)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that could be better indeed, but I do not know enough about the specifics of the tracker state / action call:

  • would renaming the method from_dict() to from_dataclass() be acceptable?
  • is the type definition I made here valid? (I marked some elements required, before they were optional)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

after thinking a bit more about it, I'm not sure that we would benefit from the dataclass here (vs keeping the TypedDict). And if we wanted to do this it might change the public API of the SDK. For instance, instead of calling custom action with plain dicts, we would call them with dataclasses. And I'm not sure it's worth the trouble. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

ok that sounds reasonable, let's stick with the typeddicts 👍

@m-vdb m-vdb requested a review from tmbo September 23, 2020 13:34
Copy link
Member

@tmbo tmbo left a comment

Choose a reason for hiding this comment

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

looks great - amazing push for better tooling 🚀

from typing_extensions import TypedDict


class TrackerState(TypedDict):
Copy link
Member

Choose a reason for hiding this comment

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

ok that sounds reasonable, let's stick with the typeddicts 👍

rasa_sdk/types.py Outdated Show resolved Hide resolved
@m-vdb m-vdb merged commit aaf8b01 into master Sep 28, 2020
@m-vdb m-vdb deleted the switch-to-mypy branch September 28, 2020 13:05
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.

Switch to mypy
2 participants