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 part0 #6470

Merged
merged 27 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4900944
remove pytype and setup mypy
m-vdb Aug 21, 2020
661ab0b
fix wrong type annotations
m-vdb Aug 21, 2020
0d35a59
update mypy config to ignore current errors
m-vdb Aug 26, 2020
684df65
Merge branch 'master' into switch-to-mypy
m-vdb Aug 26, 2020
ee42c23
Merge branch 'master' into switch-to-mypy
m-vdb Aug 31, 2020
1b553ab
Merge branch 'master' into switch-to-mypy
m-vdb Sep 2, 2020
17f125d
keep pytype around during transition
m-vdb Sep 3, 2020
199f7b9
Merge branch 'master' into switch-to-mypy
m-vdb Sep 3, 2020
c559a9f
Merge branch 'master' into switch-to-mypy
m-vdb Sep 28, 2020
077d5df
Merge branch 'master' into switch-to-mypy
m-vdb Oct 12, 2020
cef6a96
fix typing annotation
m-vdb Oct 12, 2020
85c785d
disable error codes one by one
m-vdb Oct 12, 2020
e232d1a
remove pytype completely
m-vdb Oct 14, 2020
cbb6182
Update CONTRIBUTING.md to refer to `mypy`
m-vdb Oct 14, 2020
3d0bf71
Fix type hint in DIET classifier
m-vdb Oct 14, 2020
5ffd764
Fix type hint in TED policy
m-vdb Oct 14, 2020
059dd54
explain why we ignore missing imports
m-vdb Oct 14, 2020
f1f1c01
Merge branch 'master' into switch-to-mypy
m-vdb Oct 14, 2020
7fa00f4
remove pytype annotations
m-vdb Oct 14, 2020
a7cb682
remove pytype related comments + code
m-vdb Oct 14, 2020
c6a3ec3
add changelog fragment
m-vdb Oct 14, 2020
a36d598
Revert "remove pytype annotations"
m-vdb Oct 16, 2020
8980e04
remove pytype annotations
m-vdb Oct 16, 2020
90597cc
Merge branch 'master' into switch-to-mypy
m-vdb Oct 16, 2020
4067a4b
remove more pytype comments
m-vdb Oct 19, 2020
df3b79f
Merge branch 'master' into switch-to-mypy
m-vdb Oct 19, 2020
7114fb8
remove unnecessary new lines
m-vdb Oct 19, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`.
m-vdb marked this conversation as resolved.
Show resolved Hide resolved
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))"
wochinge marked this conversation as resolved.
Show resolved Hide resolved

[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
7 changes: 3 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,12 @@ async def is_activated(
tracker: "DialogueStateTracker",
domain: "Domain",
) -> bool:
# pytype: disable=attribute-error

m-vdb marked this conversation as resolved.
Show resolved Hide resolved
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
5 changes: 1 addition & 4 deletions rasa/core/channels/botframework.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ 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

m-vdb marked this conversation as resolved.
Show resolved Hide resolved
json_message.setdefault("type", "message")
json_message.setdefault("recipient", {}).setdefault("id", recipient_id)
json_message.setdefault("from", self.bot)
Expand All @@ -161,7 +161,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 +175,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