Skip to content

Commit

Permalink
Typing updates for recent mypy versions (#481)
Browse files Browse the repository at this point in the history
* Remove redunant casts

A version of mypy has been released which includes
python/typeshed#6150. (Typeshed is vendored with mypy these days.) We
can now remove the redundant cast.

* Mark logging handler as a generic logging handler.

Since python/typeshed#5681 (vendored in recent mypy releases),
StreamHandler is now generic over its stream. This causes us pain:

> Missing type parameters for generic type "StreamHandler"

And I couldn't find a satisfactory type parameter that worked here.

- a `TimedRotatingFileHandler` instance is a `FileHandler` which is a
`StreamHandler[TextIOWrapper]`. - the instance `StreamHandler()` (which
writes to stdout) is overloaded to be a `StreamHandler[TextIO]`

`handler: StreamHandler[TextIO]` didn't work. I don't fully understand
the difference between the concrete TextIOWrapper and the abstract
TextIO (the former looks to be compatible with the latter?). I think the
StreamHandler type parameter would need to be covariant for this to
work.

In any case, we're not making use of any of the stream or file specific
attributes here. So let's just mark it as a general `Handler`.
  • Loading branch information
David Robertson committed Jan 5, 2022
1 parent 347b9f5 commit abc63a7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/481.misc
@@ -0,0 +1 @@
Update type annotations to ensure Sydent typechecks with recent mypy versions.
5 changes: 2 additions & 3 deletions sydent/db/accounts.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING, Optional, Tuple, cast
from typing import TYPE_CHECKING, Optional, Tuple

from sydent.users.accounts import Account

Expand Down Expand Up @@ -104,7 +104,6 @@ def delToken(self, token: str) -> int:
"delete from tokens where token = ?",
(token,),
)
# Cast safety: DBAPI-2 says this is a "number"; c.f. python/typeshed#6150
deleted = cast(int, cur.rowcount)
deleted = cur.rowcount
self.sydent.db.commit()
return deleted
6 changes: 2 additions & 4 deletions sydent/db/invite_tokens.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import time
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, cast
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

if TYPE_CHECKING:
from sydent.sydent import Sydent
Expand Down Expand Up @@ -137,9 +137,7 @@ def validateEphemeralPublicKey(self, publicKey: str) -> bool:
(publicKey,),
)
self.sydent.db.commit()
# Cast safety: DBAPI-2 says this is a "number"; c.f. python/typeshed#6150
rows = cast(int, cur.rowcount)
return rows > 0
return cur.rowcount > 0

def getSenderForToken(self, token: str) -> Optional[str]:
"""
Expand Down
3 changes: 2 additions & 1 deletion sydent/sydent.py
Expand Up @@ -339,8 +339,9 @@ def setup_logging(config: SydentConfig) -> None:
log_format = "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s" " - %(message)s"
formatter = logging.Formatter(log_format)

handler: logging.Handler
if log_path != "":
handler: logging.StreamHandler = logging.handlers.TimedRotatingFileHandler(
handler = logging.handlers.TimedRotatingFileHandler(
log_path, when="midnight", backupCount=365
)
handler.setFormatter(formatter)
Expand Down

0 comments on commit abc63a7

Please sign in to comment.