Patchwork [1,of,2] typing: consolidate "if not globals():" trick

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 19, 2019, 3:20 p.m.
Message ID <75589cc751d7053a9852.1574176850@mimosa>
Download mbox | patch
Permalink /patch/43361/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 19, 2019, 3:20 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1574174945 -32400
#      Tue Nov 19 23:49:05 2019 +0900
# Node ID 75589cc751d7053a9852f7de88b35ec64909a46a
# Parent  1a80d721cba5db045de98bf2cf51f5d72f4958ae
typing: consolidate "if not globals():" trick

Removes redundant inline comments. I think pycompat is good place to host
this kind of constants.
Pulkit Goyal - Nov. 25, 2019, 3:12 p.m.
On Tue, Nov 19, 2019 at 6:38 PM Yuya Nishihara <yuya@tcha.org> wrote:
>
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1574174945 -32400
> #      Tue Nov 19 23:49:05 2019 +0900
> # Node ID 75589cc751d7053a9852f7de88b35ec64909a46a
> # Parent  1a80d721cba5db045de98bf2cf51f5d72f4958ae
> typing: consolidate "if not globals():" trick
>
> Removes redundant inline comments. I think pycompat is good place to host
> this kind of constants.

Queued the series, many thanks!
>
> diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
> --- a/mercurial/branchmap.py
> +++ b/mercurial/branchmap.py
> @@ -27,7 +27,7 @@ from .utils import (
>      stringutil,
>  )
>
> -if not globals():
> +if pycompat.TYPE_CHECKING:
>      from typing import (
>          Any,
>          Callable,
> diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py
> +++ b/mercurial/cmdutil.py
> @@ -61,7 +61,7 @@ from .utils import (
>      stringutil,
>  )
>
> -if not globals():
> +if pycompat.TYPE_CHECKING:
>      from typing import (
>          Any,
>          Dict,
> diff --git a/mercurial/encoding.py b/mercurial/encoding.py
> --- a/mercurial/encoding.py
> +++ b/mercurial/encoding.py
> @@ -20,14 +20,11 @@ from . import (
>
>  from .pure import charencode as charencodepure
>
> -_TYPE_CHECKING = False
> -
> -if not globals():  # hide this from non-pytype users
> +if pycompat.TYPE_CHECKING:
>      from typing import (
>          Any,
>          Callable,
>          List,
> -        TYPE_CHECKING as _TYPE_CHECKING,
>          Text,
>          Type,
>          TypeVar,
> @@ -124,7 +121,7 @@ class localstr(bytes):
>          s._utf8 = u
>          return s
>
> -    if _TYPE_CHECKING:
> +    if pycompat.TYPE_CHECKING:
>          # pseudo implementation to help pytype see localstr() constructor
>          def __init__(self, u, l):
>              # type: (bytes, bytes) -> None
> diff --git a/mercurial/logcmdutil.py b/mercurial/logcmdutil.py
> --- a/mercurial/logcmdutil.py
> +++ b/mercurial/logcmdutil.py
> @@ -42,7 +42,7 @@ from .utils import (
>  )
>
>
> -if not globals():
> +if pycompat.TYPE_CHECKING:
>      from typing import (
>          Any,
>          Tuple,
> diff --git a/mercurial/mail.py b/mercurial/mail.py
> --- a/mercurial/mail.py
> +++ b/mercurial/mail.py
> @@ -36,7 +36,7 @@ from .utils import (
>      stringutil,
>  )
>
> -if not globals():  # hide this from non-pytype users
> +if pycompat.TYPE_CHECKING:
>      from typing import Any, List, Tuple, Union
>
>      # keep pyflakes happy
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -20,6 +20,12 @@ import tempfile
>
>  ispy3 = sys.version_info[0] >= 3
>  ispypy = '__pypy__' in sys.builtin_module_names
> +TYPE_CHECKING = False
> +
> +if not globals():  # hide this from non-pytype users
> +    import typing
> +
> +    TYPE_CHECKING = typing.TYPE_CHECKING
>
>  if not ispy3:
>      import cookielib
> diff --git a/mercurial/state.py b/mercurial/state.py
> --- a/mercurial/state.py
> +++ b/mercurial/state.py
> @@ -23,11 +23,12 @@ from .i18n import _
>
>  from . import (
>      error,
> +    pycompat,
>      util,
>  )
>  from .utils import cborutil
>
> -if not globals():
> +if pycompat.TYPE_CHECKING:
>      from typing import (
>          Any,
>          Dict,
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -27,7 +27,7 @@  from .utils import (
     stringutil,
 )
 
-if not globals():
+if pycompat.TYPE_CHECKING:
     from typing import (
         Any,
         Callable,
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -61,7 +61,7 @@  from .utils import (
     stringutil,
 )
 
-if not globals():
+if pycompat.TYPE_CHECKING:
     from typing import (
         Any,
         Dict,
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -20,14 +20,11 @@  from . import (
 
 from .pure import charencode as charencodepure
 
-_TYPE_CHECKING = False
-
-if not globals():  # hide this from non-pytype users
+if pycompat.TYPE_CHECKING:
     from typing import (
         Any,
         Callable,
         List,
-        TYPE_CHECKING as _TYPE_CHECKING,
         Text,
         Type,
         TypeVar,
@@ -124,7 +121,7 @@  class localstr(bytes):
         s._utf8 = u
         return s
 
-    if _TYPE_CHECKING:
+    if pycompat.TYPE_CHECKING:
         # pseudo implementation to help pytype see localstr() constructor
         def __init__(self, u, l):
             # type: (bytes, bytes) -> None
diff --git a/mercurial/logcmdutil.py b/mercurial/logcmdutil.py
--- a/mercurial/logcmdutil.py
+++ b/mercurial/logcmdutil.py
@@ -42,7 +42,7 @@  from .utils import (
 )
 
 
-if not globals():
+if pycompat.TYPE_CHECKING:
     from typing import (
         Any,
         Tuple,
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -36,7 +36,7 @@  from .utils import (
     stringutil,
 )
 
-if not globals():  # hide this from non-pytype users
+if pycompat.TYPE_CHECKING:
     from typing import Any, List, Tuple, Union
 
     # keep pyflakes happy
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -20,6 +20,12 @@  import tempfile
 
 ispy3 = sys.version_info[0] >= 3
 ispypy = '__pypy__' in sys.builtin_module_names
+TYPE_CHECKING = False
+
+if not globals():  # hide this from non-pytype users
+    import typing
+
+    TYPE_CHECKING = typing.TYPE_CHECKING
 
 if not ispy3:
     import cookielib
diff --git a/mercurial/state.py b/mercurial/state.py
--- a/mercurial/state.py
+++ b/mercurial/state.py
@@ -23,11 +23,12 @@  from .i18n import _
 
 from . import (
     error,
+    pycompat,
     util,
 )
 from .utils import cborutil
 
-if not globals():
+if pycompat.TYPE_CHECKING:
     from typing import (
         Any,
         Dict,