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 21 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.
125 changes: 59 additions & 66 deletions poetry.lock

Large diffs are not rendered by default.

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-16-to-rasa-17

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 # rasa-16-to-rasa-17
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 # rasa-16-to-rasa-17
except ModuleNotFoundError:
raise MissingDependencyException(
f"Rasa X does not seem to be installed, but it is needed for this CLI command."
Expand Down
9 changes: 5 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): # rasa-16-to-rasa-17
async def run(
self,
output_channel: "OutputChannel",
Expand Down Expand Up @@ -43,13 +43,14 @@ async def is_activated(
tracker: "DialogueStateTracker",
domain: "Domain",
) -> bool:
# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return tracker.active_loop_name == self.name()
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

# 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())] # rasa-16-to-rasa-17

async def activate(
self,
Expand Down
11 changes: 7 additions & 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
# rasa-16-to-rasa-17
json_message.setdefault("type", "message")
json_message.setdefault("recipient", {}).setdefault("id", recipient_id)
json_message.setdefault("from", self.bot)
Expand All @@ -161,7 +161,9 @@ async def send_custom_json(
).setdefault("alert", "true")
json_message.setdefault("text", "")
await self.send(json_message)
# pytype: enable=attribute-error


# rasa-16-to-rasa-17


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

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

# rasa-16-to-rasa-17

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 # rasa-16-to-rasa-17
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) # rasa-16-to-rasa-17

async def send_text_message(
self, recipient_id: Text, text: Text, **kwargs: Any
Expand Down
5 changes: 3 additions & 2 deletions rasa/core/channels/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,14 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return cls(
credentials.get("verify"),
credentials.get("secret"),
credentials.get("page-access-token"),
)
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

def __init__(self, fb_verify: Text, fb_secret: Text, fb_access_token: Text) -> None:
"""Create a facebook input channel.
Expand Down
2 changes: 1 addition & 1 deletion rasa/core/channels/hangouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class HangoutsInput(InputChannel):
def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChannel:

if credentials:
return cls(credentials.get("project_id")) # pytype: disable=attribute-error
return cls(credentials.get("project_id")) # rasa-16-to-rasa-17

return cls()

Expand Down
5 changes: 3 additions & 2 deletions rasa/core/channels/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if credentials is None:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
if credentials.get("pw") is not None or credentials.get("user") is not None:
rasa.shared.utils.io.raise_deprecation_warning(
"Mattermost recently switched to bot accounts. 'user' and 'pw' "
Expand All @@ -148,7 +148,8 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
token = credentials.get("token")

return cls(credentials.get("url"), token, credentials.get("webhook_url"))
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

def __init__(self, url: Text, token: Text, webhook_url: Text) -> None:
"""Create a Mattermost input channel.
Expand Down
2 changes: 1 addition & 1 deletion rasa/core/channels/rasa_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

return cls(credentials.get("url")) # pytype: disable=attribute-error
return cls(credentials.get("url")) # rasa-16-to-rasa-17

def __init__(self, url: Optional[Text]) -> None:
self.base_url = url
Expand Down
7 changes: 3 additions & 4 deletions rasa/core/channels/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def on_message_wrapper(
)
await on_new_message(message)

await queue.put("DONE") # pytype: disable=bad-return-type
await queue.put("DONE") # rasa-16-to-rasa-17

async def _extract_sender(self, req: Request) -> Optional[Text]:
return req.json.get("sender", None)
Expand All @@ -73,7 +73,6 @@ async def stream(resp: Any) -> None:
on_new_message, text, q, sender_id, input_channel, metadata
)
)
result = None # declare variable up front to avoid pytype error
while True:
result = await q.get()
if result == "DONE":
Expand All @@ -82,7 +81,7 @@ async def stream(resp: Any) -> None:
await resp.write(json.dumps(result) + "\n")
await task

return stream # pytype: disable=bad-return-type
return stream # rasa-16-to-rasa-17

def blueprint(
self, on_new_message: Callable[[UserMessage], Awaitable[None]]
Expand Down Expand Up @@ -159,4 +158,4 @@ def latest_output(self) -> NoReturn:
raise NotImplementedError("A queue doesn't allow to peek at messages.")

async def _persist_message(self, message) -> None:
await self.messages.put(message) # pytype: disable=bad-return-type
await self.messages.put(message) # rasa-16-to-rasa-17
5 changes: 3 additions & 2 deletions rasa/core/channels/rocketchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return cls(
credentials.get("user"),
credentials.get("password"),
credentials.get("server_url"),
)
# pytype: enable=attribute-error

# rasa-16-to-rasa-17
m-vdb marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, user: Text, password: Text, server_url: Text) -> None:

Expand Down
5 changes: 3 additions & 2 deletions rasa/core/channels/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return cls(
credentials.get("slack_token"),
credentials.get("slack_channel"),
Expand All @@ -138,7 +138,8 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
credentials.get("errors_ignore_retry", None),
credentials.get("use_threads", False),
)
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

def __init__(
self,
Expand Down
5 changes: 3 additions & 2 deletions rasa/core/channels/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,14 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return cls(
credentials.get("access_token"),
credentials.get("verify"),
credentials.get("webhook_url"),
)
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

def __init__(
self,
Expand Down
5 changes: 3 additions & 2 deletions rasa/core/channels/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return cls(
credentials.get("account_sid"),
credentials.get("auth_token"),
credentials.get("twilio_number"),
)
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

def __init__(
self,
Expand Down
5 changes: 3 additions & 2 deletions rasa/core/channels/webexteams.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
if not credentials:
cls.raise_missing_credentials_exception()

# pytype: disable=attribute-error
# rasa-16-to-rasa-17
return cls(credentials.get("access_token"), credentials.get("room"))
# pytype: enable=attribute-error

# rasa-16-to-rasa-17

def __init__(self, access_token: Text, room: Optional[Text] = None) -> None:
"""Create a Cisco Webex Teams input channel.
Expand Down