Skip to content

Commit

Permalink
Merge pull request #6470 from RasaHQ/switch-to-mypy
Browse files Browse the repository at this point in the history
Switch to mypy part0
  • Loading branch information
rasabot committed Oct 19, 2020
2 parents de2f1ac + 7114fb8 commit 005f8b1
Show file tree
Hide file tree
Showing 53 changed files with 151 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ venv
.ipynb_checkpoints
.ruby-version
.tox
.pytype
.mypy_cache/
dist/
pip-wheel-metadata
server/
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ If your PR is greater than 500 lines, please consider splitting it into multiple

#### Code style

To ensure a standardized code style we recommend using formatter black. To ensure our type annotations are correct we also suggest using the type checker pytype.
To ensure a standardized code style we recommend using formatter black. To ensure our type annotations are correct we also suggest using the type checker `mypy`.

#### Formatting and Type Checking

If you want to automatically format your code on every commit, you can use pre-commit. Just install it via `pip install pre-commit` and execute `pre-commit install` in the root folder. This will add a hook to the repository, which reformats files on every commit.

If you want to set it up manually, install black via `pip install -r requirements-dev.txt.` To reformat files execute `make formatter`.

If you want to check types on the codebase, install pytype using `pip install -r requirements-dev.txt`. To check the types execute `make types`.
If you want to check types on the codebase, install `mypy` using `poetry install`. To check the types execute `make types`.

The CI/CD tests that we run can be found in the [continous-integration.yml](https://github.com/RasaHQ/rasa/blob/master/.github/workflows/continous-integration.yml) file.

Expand Down
27 changes: 24 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ help:
@echo " lint"
@echo " Lint code with flake8, and check if black formatter should be applied."
@echo " types"
@echo " Check for type errors using pytype."
@echo " Check for type errors using mypy."
@echo " prepare-tests-ubuntu"
@echo " Install system requirements for running tests on Ubuntu and Debian based systems."
@echo " prepare-tests-macos"
Expand All @@ -37,7 +37,7 @@ clean:
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
rm -rf build/
rm -rf .pytype/
rm -rf .mypy_cache/
rm -rf dist/
rm -rf docs/build
rm -rf docs/.docusaurus
Expand All @@ -63,7 +63,28 @@ lint:
poetry run black --check rasa tests

types:
poetry run pytype --keep-going rasa -j 16
# FIXME: working our way towards removing these
# see https://github.com/RasaHQ/rasa/pull/6470
# the list below is sorted by the number of errors for each error code, in decreasing order
poetry run mypy rasa --disable-error-code arg-type \
--disable-error-code assignment \
--disable-error-code var-annotated \
--disable-error-code return-value \
--disable-error-code union-attr \
--disable-error-code override \
--disable-error-code operator \
--disable-error-code attr-defined \
--disable-error-code index \
--disable-error-code misc \
--disable-error-code return \
--disable-error-code call-arg \
--disable-error-code type-var \
--disable-error-code list-item \
--disable-error-code has-type \
--disable-error-code valid-type \
--disable-error-code dict-item \
--disable-error-code no-redef \
--disable-error-code func-returns-value

prepare-tests-files:
poetry install -E spacy
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ make formatter

#### Type Checking

If you want to check types on the codebase, install `pytype` using `poetry install`.
If you want to check types on the codebase, install `mypy` using `poetry install`.
To check the types execute
```
make types
Expand Down
2 changes: 2 additions & 0 deletions changelog/6470.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Python type checks are now run using `mypy` instead of `pytype`,
resulting in great performance gains and increased accuracy.
116 changes: 45 additions & 71 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.masonry.api"
[tool.black]
line-length = 88
target-version = [ "py36", "py37", "py38",]
exclude = "((.eggs | .git | .pytype | .pytest_cache | build | dist))"
exclude = "((.eggs | .git | .pytest_cache | build | dist))"

[tool.poetry]
name = "rasa"
Expand Down Expand Up @@ -143,7 +143,6 @@ fakeredis = "^1.4.0"
mongomock = "^3.18.0"
black = "^19.10b0"
flake8 = "^3.8.3"
pytype = "^2020.6.1"
google-cloud-storage = "^1.29.0"
azure-storage-blob = "<12.6.0"
coveralls = "^2.0.0"
Expand All @@ -152,6 +151,7 @@ toml = "^0.10.0"
pep440-version-utils = "^0.3.0"
pydoc-markdown = "^3.5.0"
pytest-timeout = "^1.4.2"
mypy = "^0.790"

[tool.poetry.extras]
spacy = [ "spacy",]
Expand Down
2 changes: 1 addition & 1 deletion rasa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def print_version() -> None:

python_version, os_info = sys.version.split("\n")
try:
from rasax.community.version import __version__ # pytype: disable=import-error
from rasax.community.version import __version__

rasa_x_info = __version__
except ModuleNotFoundError:
Expand Down
2 changes: 1 addition & 1 deletion rasa/cli/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@ def _get_valid_config(
)
sys.exit(1)

return config # pytype: disable=bad-return-type
return config
2 changes: 1 addition & 1 deletion rasa/cli/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def _get_credentials_and_endpoints_paths(
def run_locally(args: argparse.Namespace):
try:
# noinspection PyUnresolvedReferences
from rasax.community import local # pytype: disable=import-error
from rasax.community import local
except ModuleNotFoundError:
raise MissingDependencyException(
f"Rasa X does not seem to be installed, but it is needed for this CLI command."
Expand Down
6 changes: 2 additions & 4 deletions rasa/core/actions/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rasa.shared.core.trackers import DialogueStateTracker


class LoopAction(Action, ABC): # pytype: disable=base-class-error
class LoopAction(Action, ABC):
async def run(
self,
output_channel: "OutputChannel",
Expand Down Expand Up @@ -43,13 +43,11 @@ async def is_activated(
tracker: "DialogueStateTracker",
domain: "Domain",
) -> bool:
# pytype: disable=attribute-error
return tracker.active_loop_name == self.name()
# pytype: enable=attribute-error

# default implementation checks if form active
def _default_activation_events(self) -> List[Event]:
return [ActiveLoop(self.name())] # pytype: disable=attribute-error
return [ActiveLoop(self.name())]

async def activate(
self,
Expand Down
4 changes: 0 additions & 4 deletions rasa/core/channels/botframework.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ async def send_elements(
async def send_custom_json(
self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any
) -> None:
# pytype: disable=attribute-error
json_message.setdefault("type", "message")
json_message.setdefault("recipient", {}).setdefault("id", recipient_id)
json_message.setdefault("from", self.bot)
Expand All @@ -161,7 +160,6 @@ async def send_custom_json(
).setdefault("alert", "true")
json_message.setdefault("text", "")
await self.send(json_message)
# pytype: enable=attribute-error


class BotFrameworkInput(InputChannel):
Expand All @@ -176,9 +174,7 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
return cls(credentials.get("app_id"), credentials.get("app_password"))
# pytype: enable=attribute-error

def __init__(self, app_id: Text, app_password: Text) -> None:
"""Create a Bot Framework input channel.
Expand Down
4 changes: 2 additions & 2 deletions rasa/core/channels/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from rasa.shared.constants import DOCS_BASE_URL, DEFAULT_SENDER_ID

try:
from urlparse import urljoin # pytype: disable=import-error
from urlparse import urljoin
except ImportError:
from urllib.parse import urljoin

Expand Down Expand Up @@ -318,7 +318,7 @@ def latest_output(self) -> Optional[Dict[Text, Any]]:
return None

async def _persist_message(self, message: Dict[Text, Any]) -> None:
self.messages.append(message) # pytype: disable=bad-return-type
self.messages.append(message)

async def send_text_message(
self, recipient_id: Text, text: Text, **kwargs: Any
Expand Down

0 comments on commit 005f8b1

Please sign in to comment.