[PATCH 1 of 2] typing: correct a few missing `Optional` types where `None` is the default arg

3 views
Skip to first unread message

Matt Harbison

unread,
Nov 1, 2024, 8:41:01 PMNov 1
to thg...@googlegroups.com
# HG changeset patch
# User Matt Harbison <matt_h...@yahoo.com>
# Date 1730499620 14400
# Fri Nov 01 18:20:20 2024 -0400
# Branch stable
# Node ID cc7ff0c9089d8a1598c8696ac974771210284af7
# Parent 043700ffd1c55ce211136fe9eedd20de55131fd2
# EXP-Topic py2-to-py3-annotations
typing: correct a few missing `Optional` types where `None` is the default arg

These were caught by running the current pytype in CI (2023.11.21), but using
`PYTHON_VERSION=3.11` instead of the current default of 3.8. Running with 3.11
requires converting all of the py2 style comments to py3 style annotations, but
these are easy enough to fix first.

diff --git a/contrib/nautilus-thg.py b/contrib/nautilus-thg.py
--- a/contrib/nautilus-thg.py
+++ b/contrib/nautilus-thg.py
@@ -153,7 +153,7 @@
return None

def run_dialog(self, menuitem, hgtkcmd, cwd = None, files = None):
- # type: (Any, Text, Optional[Text], List[Text]) -> None
+ # type: (Any, Text, Optional[Text], Optional[List[Text]]) -> None
'''
hgtkcmd - hgtk subcommand
'''
diff --git a/tortoisehg/hgqt/tag.py b/tortoisehg/hgqt/tag.py
--- a/tortoisehg/hgqt/tag.py
+++ b/tortoisehg/hgqt/tag.py
@@ -57,7 +57,7 @@
class TagDialog(QDialog):

def __init__(self, repoagent, tag=b'', rev='tip', parent=None, opts=None):
- # type: (RepoAgent, bytes, Text, Optional[QWidget], Dict[Text, Union[bool, bytes]]) -> None
+ # type: (RepoAgent, bytes, Text, Optional[QWidget], Optional[Dict[Text, Union[bool, bytes]]]) -> None
super(TagDialog, self).__init__(parent)
if opts is None:
opts = {}

Matt Harbison

unread,
Nov 1, 2024, 8:41:02 PMNov 1
to thg...@googlegroups.com
# HG changeset patch
# User Matt Harbison <matt_h...@yahoo.com>
# Date 1730499758 14400
# Fri Nov 01 18:22:38 2024 -0400
# Branch stable
# Node ID 82ed5b26788385ac639501a9f55b913dbc25ca17
# Parent cc7ff0c9089d8a1598c8696ac974771210284af7
# EXP-Topic py2-to-py3-annotations
typing: add an assertion to `tortoisehg/hgqt/updatecheck.py` to help pytype

When run with `PYTHON_VERSION=3.11`, it flagged the next line with None not
having a `.close()` attr. (I don't know why it doesn't think the same thing
about the same attr in the `try` block.)

diff --git a/tortoisehg/hgqt/updatecheck.py b/tortoisehg/hgqt/updatecheck.py
--- a/tortoisehg/hgqt/updatecheck.py
+++ b/tortoisehg/hgqt/updatecheck.py
@@ -63,6 +63,7 @@
try:
data = self._newverreply.readAll().data()
finally:
+ assert self._newverreply is not None
self._newverreply.close()
self._newverreply = None


Matt Harbison

unread,
Nov 1, 2024, 8:44:34 PMNov 1
to TortoiseHg Developers
On Friday, November 1, 2024 at 8:41:01 PM UTC-4 Matt Harbison wrote:
# HG changeset patch
# User Matt Harbison <matt_h...@yahoo.com>
# Date 1730499620 14400
# Fri Nov 01 18:20:20 2024 -0400
# Branch stable
# Node ID cc7ff0c9089d8a1598c8696ac974771210284af7
# Parent 043700ffd1c55ce211136fe9eedd20de55131fd2
# EXP-Topic py2-to-py3-annotations
typing: correct a few missing `Optional` types where `None` is the default arg

These were caught by running the current pytype in CI (2023.11.21), but using
`PYTHON_VERSION=3.11` instead of the current default of 3.8. Running with 3.11
requires converting all of the py2 style comments to py3 style annotations, but
these are easy enough to fix first.

LMK if it's easier to get this py2 -> py3 translation in a large blob, individual files, or whatever.  I only inspected a handful of changes at this point because there are a lot of instances, and I'd like to do this just once:

 

Yuya Nishihara

unread,
Nov 1, 2024, 11:12:35 PMNov 1
to Matt Harbison, thg...@googlegroups.com
On Fri, 01 Nov 2024 20:40:55 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_h...@yahoo.com>
> # Date 1730499620 14400
> # Fri Nov 01 18:20:20 2024 -0400
> # Branch stable
> # Node ID cc7ff0c9089d8a1598c8696ac974771210284af7
> # Parent 043700ffd1c55ce211136fe9eedd20de55131fd2
> # EXP-Topic py2-to-py3-annotations
> typing: correct a few missing `Optional` types where `None` is the default arg

Queued this, thanks.

Yuya Nishihara

unread,
Nov 1, 2024, 11:12:37 PMNov 1
to Matt Harbison, thg...@googlegroups.com
On Fri, 01 Nov 2024 20:40:56 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_h...@yahoo.com>
> # Date 1730499758 14400
> # Fri Nov 01 18:22:38 2024 -0400
> # Branch stable
> # Node ID 82ed5b26788385ac639501a9f55b913dbc25ca17
> # Parent cc7ff0c9089d8a1598c8696ac974771210284af7
> # EXP-Topic py2-to-py3-annotations
> typing: add an assertion to `tortoisehg/hgqt/updatecheck.py` to help pytype
>
> When run with `PYTHON_VERSION=3.11`, it flagged the next line with None not
> having a `.close()` attr. (I don't know why it doesn't think the same thing
> about the same attr in the `try` block.)
>
> diff --git a/tortoisehg/hgqt/updatecheck.py b/tortoisehg/hgqt/updatecheck.py
> --- a/tortoisehg/hgqt/updatecheck.py
> +++ b/tortoisehg/hgqt/updatecheck.py
> @@ -63,6 +63,7 @@

Can we move the assertion here? I think it will express the contract more
clearly.

Matt Harbison

unread,
Nov 2, 2024, 2:43:10 AMNov 2
to thg...@googlegroups.com
# HG changeset patch
# User Matt Harbison <matt_h...@yahoo.com>
# Date 1730499758 14400
# Fri Nov 01 18:22:38 2024 -0400
# Branch stable
# Node ID ab0d1afaef06f1ac3a534db27f43c3b7622ca895
# Parent cc7ff0c9089d8a1598c8696ac974771210284af7
# EXP-Topic py2-to-py3-annotations
typing: add an assertion to `tortoisehg/hgqt/updatecheck.py` to help pytype

When run with `PYTHON_VERSION=3.11`, it flagged the first line in the `finally`
block with None not having a `.close()` attr. (I don't know why it doesn't
think the same thing about the same attr in the `try` block, and moving the
assertion to this location from inside the `finally` block required some slight
refactoring- otherwise it still thought the value could be None.)

diff --git a/tortoisehg/hgqt/updatecheck.py b/tortoisehg/hgqt/updatecheck.py
--- a/tortoisehg/hgqt/updatecheck.py
+++ b/tortoisehg/hgqt/updatecheck.py
@@ -60,10 +60,13 @@
def _uFinished(self):
newver = (0,0,0)

+ reply = self._newverreply
+ assert reply is not None
+
try:
- data = self._newverreply.readAll().data()
+ data = reply.readAll().data()
finally:
- self._newverreply.close()
+ reply.close()
self._newverreply = None

# Simulate a User-Agent string for platforms with an entry in the file.

Yuya Nishihara

unread,
Nov 2, 2024, 9:39:11 AMNov 2
to Matt Harbison, thg...@googlegroups.com
On Sat, 02 Nov 2024 02:43:04 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_h...@yahoo.com>
> # Date 1730499758 14400
> # Fri Nov 01 18:22:38 2024 -0400
> # Branch stable
> # Node ID ab0d1afaef06f1ac3a534db27f43c3b7622ca895
> # Parent cc7ff0c9089d8a1598c8696ac974771210284af7
> # EXP-Topic py2-to-py3-annotations
> typing: add an assertion to `tortoisehg/hgqt/updatecheck.py` to help pytype

Queued, thanks.
Reply all
Reply to author
Forward
0 new messages