Browse Source

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.
merge-requests/74/head
Deimos 5 years ago
parent
commit
793aeea594
  1. 3
      tildes/requirements.txt
  2. 9
      tildes/tildes/models/comment/comment.py
  3. 11
      tildes/tildes/models/topic/topic.py
  4. 2
      tildes/tildes/models/user/user.py

3
tildes/requirements.txt

@ -34,7 +34,7 @@ MarkupSafe==1.1.1
marshmallow==2.19.5 marshmallow==2.19.5
mccabe==0.6.1 mccabe==0.6.1
more-itertools==7.1.0 more-itertools==7.1.0
mypy==0.711
mypy==0.720
mypy-extensions==0.4.1 mypy-extensions==0.4.1
packaging==19.0 packaging==19.0
parso==0.5.1 parso==0.5.1
@ -101,6 +101,7 @@ traitlets==4.3.2
transaction==2.4.0 transaction==2.4.0
translationstring==1.3 translationstring==1.3
typed-ast==1.4.0 typed-ast==1.4.0
typing-extensions==3.7.4
urllib3==1.25.3 urllib3==1.25.3
venusian==1.2.0 venusian==1.2.0
waitress==1.3.0 waitress==1.3.0

9
tildes/tildes/models/comment/comment.py

@ -5,11 +5,10 @@
from collections import Counter from collections import Counter
from datetime import datetime, timedelta 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 pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone
from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text, TIMESTAMP from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text, TIMESTAMP
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import deferred, relationship from sqlalchemy.orm import deferred, relationship
from sqlalchemy.sql.expression import text from sqlalchemy.sql.expression import text
@ -23,6 +22,10 @@ from tildes.models.topic import Topic
from tildes.models.user import User from tildes.models.user import User
from tildes.schemas.comment import CommentSchema 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 # edits inside this period after creation will not mark the comment as edited
EDIT_GRACE_PERIOD = timedelta(minutes=5) EDIT_GRACE_PERIOD = timedelta(minutes=5)
@ -96,7 +99,7 @@ class Comment(DatabaseModel):
"""Return the comment's markdown.""" """Return the comment's markdown."""
return self._markdown return self._markdown
@markdown.setter # type: ignore
@markdown.setter
def markdown(self, new_markdown: str) -> None: def markdown(self, new_markdown: str) -> None:
"""Set the comment's markdown and render its HTML.""" """Set the comment's markdown and render its HTML."""
if new_markdown == self.markdown: if new_markdown == self.markdown:

11
tildes/tildes/models/topic/topic.py

@ -4,7 +4,7 @@
"""Contains the Topic class.""" """Contains the Topic class."""
from datetime import datetime, timedelta 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 pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone
from sqlalchemy import ( from sqlalchemy import (
@ -18,7 +18,6 @@ from sqlalchemy import (
TIMESTAMP, TIMESTAMP,
) )
from sqlalchemy.dialects.postgresql import ENUM, JSONB, TSVECTOR from sqlalchemy.dialects.postgresql import ENUM, JSONB, TSVECTOR
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.mutable import MutableDict from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import deferred, relationship from sqlalchemy.orm import deferred, relationship
from sqlalchemy.sql.expression import text from sqlalchemy.sql.expression import text
@ -38,6 +37,10 @@ from tildes.models.group import Group
from tildes.models.user import User from tildes.models.user import User
from tildes.schemas.topic import TITLE_MAX_LENGTH, TopicSchema 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 # edits inside this period after creation will not mark the topic as edited
EDIT_GRACE_PERIOD = timedelta(minutes=5) EDIT_GRACE_PERIOD = timedelta(minutes=5)
@ -143,7 +146,7 @@ class Topic(DatabaseModel):
return self._markdown return self._markdown
@markdown.setter # type: ignore
@markdown.setter
def markdown(self, new_markdown: str) -> None: def markdown(self, new_markdown: str) -> None:
"""Set the topic's markdown and render its HTML.""" """Set the topic's markdown and render its HTML."""
if not self.is_text_type: if not self.is_text_type:
@ -171,7 +174,7 @@ class Topic(DatabaseModel):
return sorted_tags return sorted_tags
@tags.setter # type: ignore
@tags.setter
def tags(self, new_tags: List[str]) -> None: def tags(self, new_tags: List[str]) -> None:
self._tags = new_tags self._tags = new_tags

2
tildes/tildes/models/user/user.py

@ -172,7 +172,7 @@ class User(DatabaseModel):
def __init__(self, username: str, password: str): def __init__(self, username: str, password: str):
"""Create a new user account.""" """Create a new user account."""
self.username = username self.username = username
self.password = password
self.password = password # type: ignore
def __acl__(self) -> Sequence[Tuple[str, Any, str]]: def __acl__(self) -> Sequence[Tuple[str, Any, str]]:
"""Pyramid security ACL.""" """Pyramid security ACL."""

Loading…
Cancel
Save