From 793aeea594a9dac46970aee4dca2ab68b39ed351 Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 18 Jul 2019 20:01:33 -0600 Subject: [PATCH] Update mypy to 0.720 mypy 0.720 includes a new semantic analyzer that it uses by default. It mostly still works fine with Tildes's code, except that it doesn't understand that @hybrid_property is very similar to @property, and it also fixes a bug I was taking advantage of before to get mypy to not complain about a @property that returns a different type than you set it to. This uses the TYPE_CHECKING constant (which is only ever true when mypy is currently analyzing the code) to effectively replace @hybrid_property with @property so that mypy understands it. --- tildes/requirements.txt | 3 ++- tildes/tildes/models/comment/comment.py | 9 ++++++--- tildes/tildes/models/topic/topic.py | 11 +++++++---- tildes/tildes/models/user/user.py | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/tildes/requirements.txt b/tildes/requirements.txt index d396c45..b500f83 100644 --- a/tildes/requirements.txt +++ b/tildes/requirements.txt @@ -34,7 +34,7 @@ MarkupSafe==1.1.1 marshmallow==2.19.5 mccabe==0.6.1 more-itertools==7.1.0 -mypy==0.711 +mypy==0.720 mypy-extensions==0.4.1 packaging==19.0 parso==0.5.1 @@ -101,6 +101,7 @@ traitlets==4.3.2 transaction==2.4.0 translationstring==1.3 typed-ast==1.4.0 +typing-extensions==3.7.4 urllib3==1.25.3 venusian==1.2.0 waitress==1.3.0 diff --git a/tildes/tildes/models/comment/comment.py b/tildes/tildes/models/comment/comment.py index 0044404..38c190d 100644 --- a/tildes/tildes/models/comment/comment.py +++ b/tildes/tildes/models/comment/comment.py @@ -5,11 +5,10 @@ from collections import Counter from datetime import datetime, timedelta -from typing import Any, Optional, Sequence, Tuple +from typing import Any, Optional, Sequence, Tuple, TYPE_CHECKING from pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text, TIMESTAMP -from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import deferred, relationship from sqlalchemy.sql.expression import text @@ -23,6 +22,10 @@ from tildes.models.topic import Topic from tildes.models.user import User from tildes.schemas.comment import CommentSchema +if TYPE_CHECKING: # workaround for mypy issues with @hybrid_property + from builtins import property as hybrid_property +else: + from sqlalchemy.ext.hybrid import hybrid_property # edits inside this period after creation will not mark the comment as edited EDIT_GRACE_PERIOD = timedelta(minutes=5) @@ -96,7 +99,7 @@ class Comment(DatabaseModel): """Return the comment's markdown.""" return self._markdown - @markdown.setter # type: ignore + @markdown.setter def markdown(self, new_markdown: str) -> None: """Set the comment's markdown and render its HTML.""" if new_markdown == self.markdown: diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index 1f8119a..a030d70 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -4,7 +4,7 @@ """Contains the Topic class.""" from datetime import datetime, timedelta -from typing import Any, Dict, List, Optional, Sequence, Tuple +from typing import Any, Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING from pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone from sqlalchemy import ( @@ -18,7 +18,6 @@ from sqlalchemy import ( TIMESTAMP, ) from sqlalchemy.dialects.postgresql import ENUM, JSONB, TSVECTOR -from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.mutable import MutableDict from sqlalchemy.orm import deferred, relationship from sqlalchemy.sql.expression import text @@ -38,6 +37,10 @@ from tildes.models.group import Group from tildes.models.user import User from tildes.schemas.topic import TITLE_MAX_LENGTH, TopicSchema +if TYPE_CHECKING: # workaround for mypy issues with @hybrid_property + from builtins import property as hybrid_property +else: + from sqlalchemy.ext.hybrid import hybrid_property # edits inside this period after creation will not mark the topic as edited EDIT_GRACE_PERIOD = timedelta(minutes=5) @@ -143,7 +146,7 @@ class Topic(DatabaseModel): return self._markdown - @markdown.setter # type: ignore + @markdown.setter def markdown(self, new_markdown: str) -> None: """Set the topic's markdown and render its HTML.""" if not self.is_text_type: @@ -171,7 +174,7 @@ class Topic(DatabaseModel): return sorted_tags - @tags.setter # type: ignore + @tags.setter def tags(self, new_tags: List[str]) -> None: self._tags = new_tags diff --git a/tildes/tildes/models/user/user.py b/tildes/tildes/models/user/user.py index d30679d..d705595 100644 --- a/tildes/tildes/models/user/user.py +++ b/tildes/tildes/models/user/user.py @@ -172,7 +172,7 @@ class User(DatabaseModel): def __init__(self, username: str, password: str): """Create a new user account.""" self.username = username - self.password = password + self.password = password # type: ignore def __acl__(self) -> Sequence[Tuple[str, Any, str]]: """Pyramid security ACL."""