kenoss / pyproject-indirect-import-detector

Indirct-import detector using pyproject.toml
MIT License
2 stars 0 forks source link

fix: Black fails #1

Open kenoss opened 3 years ago

kenoss commented 3 years ago

CI: https://app.circleci.com/pipelines/github/kenoss/pyproject-indirect-import-detector/20/workflows/8a28ef32-2449-4fac-b779-f32f763fd66f/jobs/73

$ poetry run pysen run lint
Consider specifying 'version' under [tool.pysen] section in your pyproject.toml to check compliance against the version of the installed pysen. (File: /home/keno/src/github.com/kenoss/pyproject-indirect-import-detector/pyproject.toml)
Running commands concurrently...
... concurrent execution done
Running: black
Checking 20 files
error: cannot format /home/keno/src/github.com/kenoss/pyproject-indirect-import-detector/src/pyproject_indirect_import_detector/util.py: INTERNAL ERROR: Black produced invalid code: invalid syntax (<unknown>, line 8). Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: /tmp/blk_dtrwzz9g.log
error: cannot format /home/keno/src/github.com/kenoss/pyproject-indirect-import-detector/src/pyproject_indirect_import_detector/domain.py: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /tmp/blk_kz41nbnt.log
error: cannot format /home/keno/src/github.com/kenoss/pyproject-indirect-import-detector/src/pyproject_indirect_import_detector/pyproject.py: INTERNAL ERROR: Black produced invalid code: invalid syntax (<unknown>, line 27). Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: /tmp/blk_8gw6nbc6.log
Oh no! πŸ’₯ πŸ’” πŸ’₯
17 files would be left unchanged, 3 files would fail to reformat.
Running: flake8
Checking 20 files
Running: isort
Checking 20 files
Running: mypy
[1/1] Checking 1 entries
Success: no issues found in 9 source files

 ** execution summary **
isort .......... OK (0.24 sec)
black .......... Failed (0.27 sec)
flake8 .......... OK (0.25 sec)
mypy .......... OK (0.16 sec)

lint finished with error(s)
Errored:
 - black
$ cat /tmp/blk_dtrwzz9g.log
  File "/home/keno/.cache/pypoetry/virtualenvs/pyproject-indirect-import-detector-7fwl-yUj-py3.9/lib/python3.9/site-packages/black/__init__.py", line 6141, in assert_equivalent
    dst_ast = parse_ast(dst)
  File "/home/keno/.cache/pypoetry/virtualenvs/pyproject-indirect-import-detector-7fwl-yUj-py3.9/lib/python3.9/site-packages/black/__init__.py", line 6052, in parse_ast
    return ast27.parse(src)
  File "/home/keno/.cache/pypoetry/virtualenvs/pyproject-indirect-import-detector-7fwl-yUj-py3.9/lib/python3.9/site-packages/typed_ast/ast27.py", line 50, in parse
    return _ast27.parse(source, filename, mode)
import os
from pathlib import Path
from typing import Any, List, TypeVar

T = TypeVar("T")

def _flatten(xss: List[List[T]]) -> List[T]:
    # fmt: off
    return [x
            for xs in xss
            for x in xs]

    def _walk(path: Path) -> List[Path]:
        # fmt: off
    return [Path(d) / f
            for (d, _, fs) in os.walk(path)
            for f in fs]

        def _list_all_python_files(path: Path) -> List[Path]:
            # fmt: off
    return [path
            for path in _walk(path)
            if path.name.endswith(".py")]

            def _dict_rec_get(d: dict[Any, Any], path: List[Any], default: Any) -> Any:  # type: ignore  # reason: dict
                """
                Get an element of path from dict.

                >>> d = {'a': 'a', 'b': {'c': 'bc', 'd': {'e': 'bde'}}}

                Simple get:

                >>> _dict_rec_get(d, ['a'], None)
                'a'

                Returns default if key does not exist:

                >>> _dict_rec_get(d, ['c'], None) is None
                True
                >>> _dict_rec_get(d, ['c'], 0)
                0

                Get recursive:

                >>> _dict_rec_get(d, ['b', 'c'], None)
                'bc'
                >>> _dict_rec_get(d, ['b', 'd'], None)
                {'e': 'bde'}
                >>> _dict_rec_get(d, ['b', 'd', 'e'], None)
                'bde'
                >>> _dict_rec_get(d, ['b', 'nopath'], None) is None
                True
                """

                assert isinstance(path, list)

                while len(path) != 0:
                    p = path[0]
                    path = path[1:]
                    if isinstance(d, dict) and (p in d):  # type: ignore
                        d = d[p]
                    else:
                        return default

                return d
$ cat /tmp/blk_kz41nbnt.log
--- src
+++ dst
@@ -710,10 +710,599 @@
                         'dir_',  # str
                     )  # /Name
                 )  # /comprehension
             )  # /ListComp
         )  # /Return
+        FunctionDef(
+          args=
+            arguments(
+              args=
+                arg(
+                  annotation=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'str',  # str
+                    )  # /Name
+                  arg=
+                    'self_project_name',  # str
+                  type_comment=
+                    None,  # NoneType
+                )  # /arg
+                arg(
+                  annotation=
+                    Subscript(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      slice=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'str',  # str
+                        )  # /Name
+                      value=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'List',  # str
+                        )  # /Name
+                    )  # /Subscript
+                  arg=
+                    'self_module_names',  # str
+                  type_comment=
+                    None,  # NoneType
+                )  # /arg
+                arg(
+                  annotation=
+                    Subscript(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      slice=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'str',  # str
+                        )  # /Name
+                      value=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'List',  # str
+                        )  # /Name
+                    )  # /Subscript
+                  arg=
+                    'project_names',  # str
+                  type_comment=
+                    None,  # NoneType
+                )  # /arg
+                arg(
+                  annotation=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'str',  # str
+                    )  # /Name
+                  arg=
+                    'python_version',  # str
+                  type_comment=
+                    None,  # NoneType
+                )  # /arg
+                arg(
+                  annotation=
+                    Subscript(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      slice=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'str',  # str
+                        )  # /Name
+                      value=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'List',  # str
+                        )  # /Name
+                    )  # /Subscript
+                  arg=
+                    'exclude_modules',  # str
+                  type_comment=
+                    None,  # NoneType
+                )  # /arg
+              defaults=
+              kw_defaults=
+              kwarg=
+                None,  # NoneType
+              kwonlyargs=
+              posonlyargs=
+              vararg=
+                None,  # NoneType
+            )  # /arguments
+          body=
+            Try(
+              body=
+                Assign(
+                  targets=
+                    Name(
+                      ctx=
+                        Store(
+                        )  # /Store
+                      id=
+                        'modules_std',  # str
+                    )  # /Name
+                  type_comment=
+                    None,  # NoneType
+                  value=
+                    Call(
+                      args=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'python_version',  # str
+                        )  # /Name
+                      func=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'stdlib_list',  # str
+                        )  # /Name
+                      keywords=
+                    )  # /Call
+                )  # /Assign
+              finalbody=
+              handlers=
+                ExceptHandler(
+                  body=
+                    Assign(
+                      targets=
+                        Name(
+                          ctx=
+                            Store(
+                            )  # /Store
+                          id=
+                            'msg',  # str
+                        )  # /Name
+                      type_comment=
+                        None,  # NoneType
+                      value=
+                        JoinedStr(
+                          values=
+                            Constant(
+                              kind=
+                                None,  # NoneType
+                              value=
+                                '`stdlib-list` does not support',  # str
+                            )  # /Constant
+                            FormattedValue(
+                              conversion=
+                                -1,  # int
+                              format_spec=
+                                None,  # NoneType
+                              value=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    'python_version',  # str
+                                )  # /Name
+                            )  # /FormattedValue
+                        )  # /JoinedStr
+                    )  # /Assign
+                    Return(
+                      value=
+                        Call(
+                          args=
+                            Call(
+                              args=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    'msg',  # str
+                                )  # /Name
+                              func=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    'InvalidPythonVersionError',  # str
+                                )  # /Name
+                              keywords=
+                            )  # /Call
+                          func=
+                            Attribute(
+                              attr=
+                                'wrap_err',  # str
+                              ctx=
+                                Load(
+                                )  # /Load
+                              value=
+                                Call(
+                                  args=
+                                    Name(
+                                      ctx=
+                                        Load(
+                                        )  # /Load
+                                      id=
+                                        'err',  # str
+                                    )  # /Name
+                                  func=
+                                    Name(
+                                      ctx=
+                                        Load(
+                                        )  # /Load
+                                      id=
+                                        'Err',  # str
+                                    )  # /Name
+                                  keywords=
+                                )  # /Call
+                            )  # /Attribute
+                          keywords=
+                        )  # /Call
+                    )  # /Return
+                  name=
+                    'err',  # str
+                  type=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'ValueError',  # str
+                    )  # /Name
+                )  # /ExceptHandler
+              orelse=
+            )  # /Try
+            Assign(
+              targets=
+                Name(
+                  ctx=
+                    Store(
+                    )  # /Store
+                  id=
+                    'proj_to_modules_std',  # str
+                )  # /Name
+              type_comment=
+                None,  # NoneType
+              value=
+                Dict(
+                  keys=
+                    Constant(
+                      kind=
+                        None,  # NoneType
+                      value=
+                        '<std>',  # str
+                    )  # /Constant
+                  values=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'modules_std',  # str
+                    )  # /Name
+                )  # /Dict
+            )  # /Assign
+            Assign(
+              targets=
+                Name(
+                  ctx=
+                    Store(
+                    )  # /Store
+                  id=
+                    'proj_to_modules_dep',  # str
+                )  # /Name
+              type_comment=
+                None,  # NoneType
+              value=
+                Call(
+                  args=
+                    GeneratorExp(
+                      elt=
+                        Tuple(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          elts=
+                            Name(
+                              ctx=
+                                Load(
+                                )  # /Load
+                              id=
+                                'proj',  # str
+                            )  # /Name
+                            Call(
+                              args=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    'proj',  # str
+                                )  # /Name
+                              func=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    '_get_modules',  # str
+                                )  # /Name
+                              keywords=
+                            )  # /Call
+                        )  # /Tuple
+                      generators=
+                        comprehension(
+                          ifs=
+                          is_async=
+                            0,  # int
+                          iter=
+                            Name(
+                              ctx=
+                                Load(
+                                )  # /Load
+                              id=
+                                'project_names',  # str
+                            )  # /Name
+                          target=
+                            Name(
+                              ctx=
+                                Store(
+                                )  # /Store
+                              id=
+                                'proj',  # str
+                            )  # /Name
+                        )  # /comprehension
+                    )  # /GeneratorExp
+                  func=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'dict',  # str
+                    )  # /Name
+                  keywords=
+                )  # /Call
+            )  # /Assign
+            Assign(
+              targets=
+                Name(
+                  ctx=
+                    Store(
+                    )  # /Store
+                  id=
+                    'proj_to_modules_self',  # str
+                )  # /Name
+              type_comment=
+                None,  # NoneType
+              value=
+                Dict(
+                  keys=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'self_project_name',  # str
+                    )  # /Name
+                  values=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'self_module_names',  # str
+                    )  # /Name
+                )  # /Dict
+            )  # /Assign
+            Assign(
+              targets=
+                Name(
+                  ctx=
+                    Store(
+                    )  # /Store
+                  id=
+                    'proj_to_modules_exclude',  # str
+                )  # /Name
+              type_comment=
+                None,  # NoneType
+              value=
+                Dict(
+                  keys=
+                    Constant(
+                      kind=
+                        None,  # NoneType
+                      value=
+                        '<exclude>',  # str
+                    )  # /Constant
+                  values=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'exclude_modules',  # str
+                    )  # /Name
+                )  # /Dict
+            )  # /Assign
+            Assign(
+              targets=
+                Name(
+                  ctx=
+                    Store(
+                    )  # /Store
+                  id=
+                    'proj_to_modules',  # str
+                )  # /Name
+              type_comment=
+                None,  # NoneType
+              value=
+                Dict(
+                  keys=
+                  values=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'proj_to_modules_std',  # str
+                    )  # /Name
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'proj_to_modules_dep',  # str
+                    )  # /Name
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'proj_to_modules_self',  # str
+                    )  # /Name
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'proj_to_modules_exclude',  # str
+                    )  # /Name
+                )  # /Dict
+            )  # /Assign
+            Return(
+              value=
+                Call(
+                  args=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'proj_to_modules',  # str
+                    )  # /Name
+                  func=
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'Ok',  # str
+                    )  # /Name
+                  keywords=
+                )  # /Call
+            )  # /Return
+          decorator_list=
+          name=
+            '_load_proj_to_modules',  # str
+          returns=
+            Subscript(
+              ctx=
+                Load(
+                )  # /Load
+              slice=
+                Tuple(
+                  ctx=
+                    Load(
+                    )  # /Load
+                  elts=
+                    Subscript(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      slice=
+                        Tuple(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          elts=
+                            Name(
+                              ctx=
+                                Load(
+                                )  # /Load
+                              id=
+                                'str',  # str
+                            )  # /Name
+                            Subscript(
+                              ctx=
+                                Load(
+                                )  # /Load
+                              slice=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    'str',  # str
+                                )  # /Name
+                              value=
+                                Name(
+                                  ctx=
+                                    Load(
+                                    )  # /Load
+                                  id=
+                                    'List',  # str
+                                )  # /Name
+                            )  # /Subscript
+                        )  # /Tuple
+                      value=
+                        Name(
+                          ctx=
+                            Load(
+                            )  # /Load
+                          id=
+                            'dict',  # str
+                        )  # /Name
+                    )  # /Subscript
+                    Name(
+                      ctx=
+                        Load(
+                        )  # /Load
+                      id=
+                        'InvalidPythonVersionError',  # str
+                    )  # /Name
+                )  # /Tuple
+              value=
+                Name(
+                  ctx=
+                    Load(
+                    )  # /Load
+                  id=
+                    'Result',  # str
+                )  # /Name
+            )  # /Subscript
+          type_comment=
+            None,  # NoneType
+        )  # /FunctionDef
       decorator_list=
       name=
         '_get_modules_by_files',  # str
       returns=
         Subscript(
@@ -738,596 +1327,7 @@
             )  # /Name
         )  # /Subscript
       type_comment=
         None,  # NoneType
     )  # /FunctionDef
-    FunctionDef(
-      args=
-        arguments(
-          args=
-            arg(
-              annotation=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'str',  # str
-                )  # /Name
-              arg=
-                'self_project_name',  # str
-              type_comment=
-                None,  # NoneType
-            )  # /arg
-            arg(
-              annotation=
-                Subscript(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  slice=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'str',  # str
-                    )  # /Name
-                  value=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'List',  # str
-                    )  # /Name
-                )  # /Subscript
-              arg=
-                'self_module_names',  # str
-              type_comment=
-                None,  # NoneType
-            )  # /arg
-            arg(
-              annotation=
-                Subscript(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  slice=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'str',  # str
-                    )  # /Name
-                  value=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'List',  # str
-                    )  # /Name
-                )  # /Subscript
-              arg=
-                'project_names',  # str
-              type_comment=
-                None,  # NoneType
-            )  # /arg
-            arg(
-              annotation=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'str',  # str
-                )  # /Name
-              arg=
-                'python_version',  # str
-              type_comment=
-                None,  # NoneType
-            )  # /arg
-            arg(
-              annotation=
-                Subscript(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  slice=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'str',  # str
-                    )  # /Name
-                  value=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'List',  # str
-                    )  # /Name
-                )  # /Subscript
-              arg=
-                'exclude_modules',  # str
-              type_comment=
-                None,  # NoneType
-            )  # /arg
-          defaults=
-          kw_defaults=
-          kwarg=
-            None,  # NoneType
-          kwonlyargs=
-          posonlyargs=
-          vararg=
-            None,  # NoneType
-        )  # /arguments
-      body=
-        Try(
-          body=
-            Assign(
-              targets=
-                Name(
-                  ctx=
-                    Store(
-                    )  # /Store
-                  id=
-                    'modules_std',  # str
-                )  # /Name
-              type_comment=
-                None,  # NoneType
-              value=
-                Call(
-                  args=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'python_version',  # str
-                    )  # /Name
-                  func=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'stdlib_list',  # str
-                    )  # /Name
-                  keywords=
-                )  # /Call
-            )  # /Assign
-          finalbody=
-          handlers=
-            ExceptHandler(
-              body=
-                Assign(
-                  targets=
-                    Name(
-                      ctx=
-                        Store(
-                        )  # /Store
-                      id=
-                        'msg',  # str
-                    )  # /Name
-                  type_comment=
-                    None,  # NoneType
-                  value=
-                    JoinedStr(
-                      values=
-                        Constant(
-                          kind=
-                            None,  # NoneType
-                          value=
-                            '`stdlib-list` does not support',  # str
-                        )  # /Constant
-                        FormattedValue(
-                          conversion=
-                            -1,  # int
-                          format_spec=
-                            None,  # NoneType
-                          value=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'python_version',  # str
-                            )  # /Name
-                        )  # /FormattedValue
-                    )  # /JoinedStr
-                )  # /Assign
-                Return(
-                  value=
-                    Call(
-                      args=
-                        Call(
-                          args=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'msg',  # str
-                            )  # /Name
-                          func=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'InvalidPythonVersionError',  # str
-                            )  # /Name
-                          keywords=
-                        )  # /Call
-                      func=
-                        Attribute(
-                          attr=
-                            'wrap_err',  # str
-                          ctx=
-                            Load(
-                            )  # /Load
-                          value=
-                            Call(
-                              args=
-                                Name(
-                                  ctx=
-                                    Load(
-                                    )  # /Load
-                                  id=
-                                    'err',  # str
-                                )  # /Name
-                              func=
-                                Name(
-                                  ctx=
-                                    Load(
-                                    )  # /Load
-                                  id=
-                                    'Err',  # str
-                                )  # /Name
-                              keywords=
-                            )  # /Call
-                        )  # /Attribute
-                      keywords=
-                    )  # /Call
-                )  # /Return
-              name=
-                'err',  # str
-              type=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'ValueError',  # str
-                )  # /Name
-            )  # /ExceptHandler
-          orelse=
-        )  # /Try
-        Assign(
-          targets=
-            Name(
-              ctx=
-                Store(
-                )  # /Store
-              id=
-                'proj_to_modules_std',  # str
-            )  # /Name
-          type_comment=
-            None,  # NoneType
-          value=
-            Dict(
-              keys=
-                Constant(
-                  kind=
-                    None,  # NoneType
-                  value=
-                    '<std>',  # str
-                )  # /Constant
-              values=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'modules_std',  # str
-                )  # /Name
-            )  # /Dict
-        )  # /Assign
-        Assign(
-          targets=
-            Name(
-              ctx=
-                Store(
-                )  # /Store
-              id=
-                'proj_to_modules_dep',  # str
-            )  # /Name
-          type_comment=
-            None,  # NoneType
-          value=
-            Call(
-              args=
-                GeneratorExp(
-                  elt=
-                    Tuple(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      elts=
-                        Name(
-                          ctx=
-                            Load(
-                            )  # /Load
-                          id=
-                            'proj',  # str
-                        )  # /Name
-                        Call(
-                          args=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'proj',  # str
-                            )  # /Name
-                          func=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                '_get_modules',  # str
-                            )  # /Name
-                          keywords=
-                        )  # /Call
-                    )  # /Tuple
-                  generators=
-                    comprehension(
-                      ifs=
-                      is_async=
-                        0,  # int
-                      iter=
-                        Name(
-                          ctx=
-                            Load(
-                            )  # /Load
-                          id=
-                            'project_names',  # str
-                        )  # /Name
-                      target=
-                        Name(
-                          ctx=
-                            Store(
-                            )  # /Store
-                          id=
-                            'proj',  # str
-                        )  # /Name
-                    )  # /comprehension
-                )  # /GeneratorExp
-              func=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'dict',  # str
-                )  # /Name
-              keywords=
-            )  # /Call
-        )  # /Assign
-        Assign(
-          targets=
-            Name(
-              ctx=
-                Store(
-                )  # /Store
-              id=
-                'proj_to_modules_self',  # str
-            )  # /Name
-          type_comment=
-            None,  # NoneType
-          value=
-            Dict(
-              keys=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'self_project_name',  # str
-                )  # /Name
-              values=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'self_module_names',  # str
-                )  # /Name
-            )  # /Dict
-        )  # /Assign
-        Assign(
-          targets=
-            Name(
-              ctx=
-                Store(
-                )  # /Store
-              id=
-                'proj_to_modules_exclude',  # str
-            )  # /Name
-          type_comment=
-            None,  # NoneType
-          value=
-            Dict(
-              keys=
-                Constant(
-                  kind=
-                    None,  # NoneType
-                  value=
-                    '<exclude>',  # str
-                )  # /Constant
-              values=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'exclude_modules',  # str
-                )  # /Name
-            )  # /Dict
-        )  # /Assign
-        Assign(
-          targets=
-            Name(
-              ctx=
-                Store(
-                )  # /Store
-              id=
-                'proj_to_modules',  # str
-            )  # /Name
-          type_comment=
-            None,  # NoneType
-          value=
-            Dict(
-              keys=
-              values=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'proj_to_modules_std',  # str
-                )  # /Name
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'proj_to_modules_dep',  # str
-                )  # /Name
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'proj_to_modules_self',  # str
-                )  # /Name
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'proj_to_modules_exclude',  # str
-                )  # /Name
-            )  # /Dict
-        )  # /Assign
-        Return(
-          value=
-            Call(
-              args=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'proj_to_modules',  # str
-                )  # /Name
-              func=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'Ok',  # str
-                )  # /Name
-              keywords=
-            )  # /Call
-        )  # /Return
-      decorator_list=
-      name=
-        '_load_proj_to_modules',  # str
-      returns=
-        Subscript(
-          ctx=
-            Load(
-            )  # /Load
-          slice=
-            Tuple(
-              ctx=
-                Load(
-                )  # /Load
-              elts=
-                Subscript(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  slice=
-                    Tuple(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      elts=
-                        Name(
-                          ctx=
-                            Load(
-                            )  # /Load
-                          id=
-                            'str',  # str
-                        )  # /Name
-                        Subscript(
-                          ctx=
-                            Load(
-                            )  # /Load
-                          slice=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'str',  # str
-                            )  # /Name
-                          value=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'List',  # str
-                            )  # /Name
-                        )  # /Subscript
-                    )  # /Tuple
-                  value=
-                    Name(
-                      ctx=
-                        Load(
-                        )  # /Load
-                      id=
-                        'dict',  # str
-                    )  # /Name
-                )  # /Subscript
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'InvalidPythonVersionError',  # str
-                )  # /Name
-            )  # /Tuple
-          value=
-            Name(
-              ctx=
-                Load(
-                )  # /Load
-              id=
-                'Result',  # str
-            )  # /Name
-        )  # /Subscript
-      type_comment=
-        None,  # NoneType
-    )  # /FunctionDef
   type_ignores=
 )  # /Module
$ cat /tmp/blk_8gw6nbc6.log
  File "/home/keno/.cache/pypoetry/virtualenvs/pyproject-indirect-import-detector-7fwl-yUj-py3.9/lib/python3.9/site-packages/black/__init__.py", line 6141, in assert_equivalent
    dst_ast = parse_ast(dst)
  File "/home/keno/.cache/pypoetry/virtualenvs/pyproject-indirect-import-detector-7fwl-yUj-py3.9/lib/python3.9/site-packages/black/__init__.py", line 6052, in parse_ast
    return ast27.parse(src)
  File "/home/keno/.cache/pypoetry/virtualenvs/pyproject-indirect-import-detector-7fwl-yUj-py3.9/lib/python3.9/site-packages/typed_ast/ast27.py", line 50, in parse
    return _ast27.parse(source, filename, mode)
import logging as _logging

# noqa idiom
if True:
    logger = _logging.getLogger(__name__)
    handler = _logging.StreamHandler()
    _level = _logging.WARNING
    handler.setLevel(_level)
    logger.setLevel(_level)
    logger.addHandler(handler)
    logger.propagate = False

from pathlib import Path
from typing import Any, List, MutableMapping, Tuple, cast

import toml
from pkg_resources import to_filename

from .domain import _load_proj_to_modules
from .exception import InvalidPyProjectError, InvalidPythonVersionError
from .result import Err, Ok, Result
from .util import _dict_rec_get, _flatten

class _PyProject:
    _t: dict[str, Any]  # type: ignore  # reason: dict

    def __init__(self, t: MutableMapping[str, Any]) -> None:
        self._t = cast(dict[str, Any], t)  # type: ignore  # reason: dict

    def _init_validate(self) -> Result[None, InvalidPyProjectError]:
        if _dict_rec_get(self._t, ["tool", "poetry", "name"], None) is None:
            return Err(InvalidPyProjectError('path "tool/poetry/name" must not be empty'))

        res = self.base_python_version()
        if res.is_err():
            return Err(res.unwrap_err())

        return Ok(None)

    @classmethod
    def load(cls, path: Path) -> Result["_PyProject", InvalidPyProjectError]:
        path = path / "pyproject.toml"
        t = toml.load(open(path))
        this = cls(t)
        res = this._init_validate()
        if res.is_err():
            return Err(res.unwrap_err())
        return Ok(this)

    def _project_name(self) -> str:
        ret = self._t["tool"]["poetry"]["name"]
        assert type(ret) is str
        return cast(str, ret)

    def _project_name_normalized(self) -> str:
        return to_filename(self._project_name())

    def _module_names(self) -> List[str]:
        packages = [x for (x, _) in self._packages_in_src()]
        return sorted(list(set(_flatten([[self._project_name_normalized()], packages]))))

    def _packages_in_src(self) -> List[Tuple[str, Path]]:
        packages__ = _dict_rec_get(self._t, ["tool", "poetry", "packages"], None)
        packages_ = [] if packages__ is None else packages__
        # fmt: off
        packages = [(x["include"], Path(x["from"]) / x["include"])
                    for x in packages_
                    # Shoud we check `type(x) is ...`?
                    if ("from" in x) and ("include" in x)]
        return packages

        def _exclude_modules(self) -> List[str]:
            modules = _dict_rec_get(self._t, ["tool", "pyproject-indirect-import-detector", "exclude_modules"], [])
            assert type(modules) is list
            for module in modules:
                assert type(module) is str
            return cast(List[str], modules)

        def base_python_version(self) -> Result[str, InvalidPythonVersionError]:
            python_version_constraint = _dict_rec_get(self._t, ["tool", "poetry", "dependencies", "python"], None)
            if python_version_constraint is None:
                return Err(InvalidPythonVersionError("must not be empty."))
            return _parse_minimal_python_verison(python_version_constraint)

        def _dependencies(self) -> List[str]:
            deps = _dict_rec_get(self._t, ["tool", "poetry", "dependencies"], None)
            if deps is None:
                return []
            else:
                return list(deps.keys())

        def _dev_dependencies(self) -> List[str]:
            deps = _dict_rec_get(self._t, ["tool", "poetry", "dev-dependencies"], None)
            if deps is None:
                return []
            else:
                return list(deps.keys())

        def dependencies(self, include_dev: bool) -> List[str]:
            xs = self._dependencies()
            if include_dev:
                xs += self._dev_dependencies()

            xs = list(set(xs))
            xs.remove("python")
            xs.sort()
            return xs

        def load_module_to_proj(self, dev: bool) -> Result[dict[str, str], Exception]:  # type: ignore  # reason: dict
            python_version_ = self.base_python_version()
            if python_version_.is_err():
                return Err(python_version_.unwrap_err())
            python_version = python_version_.unwrap()

            proj_to_modules_ = _load_proj_to_modules(
                self._project_name(),
                self._module_names(),
                self.dependencies(dev),
                python_version,
                self._exclude_modules(),
            )
            if proj_to_modules_.is_err():
                return Err(proj_to_modules_.unwrap_err())
            proj_to_modules = proj_to_modules_.unwrap()
            logger.debug(f"proj_to_modules = {proj_to_modules}")

            # fmt: off
        module_to_proj = dict((m, p)
                              for (p, ms) in proj_to_modules.items()
                              for m in ms)
        logger.debug(f"module_to_proj = {module_to_proj}")
        return Ok(module_to_proj)

            # Separate method for test.
            def _target_dirs(self, dev: bool) -> List[Path]:
                if dev:
                    # TODO: Should we make it configurable?
                    paths_ = ["tests"]
                    paths = [Path(path) for path in paths_]
                else:
                    packages = self._packages_in_src()
                    if len(packages) == 0:
                        # Case: Module is `<package_name>`

                        paths = [Path(self._project_name_normalized())]
                    else:
                        # Case: Modules are `src/<module>`

                        paths = [x for (_, x) in packages]

                return paths

            def target_dirs(self, dev: bool) -> List[Path]:
                return [path for path in self._target_dirs(dev) if path.exists()]

        def _parse_minimal_python_verison(spec: str) -> Result[str, InvalidPythonVersionError]:
            def normalize(version: str) -> str:
                version_ = version.split(".")[:2]
                if len(version_) == 0:
                    raise RuntimeError("unreachable")
                elif len(version_) == 1:
                    return version_[0] + ".0"
                else:
                    return version_[0] + "." + version_[1]

            if spec.startswith("^"):
                return Ok(normalize(spec[1:]))
            elif spec.startswith("~"):
                return Ok(normalize(spec[1:]))
            elif spec.endswith(".*"):
                return Ok(normalize(spec[:-2]))
            elif spec == "*":
                return Err(
                    InvalidPythonVersionError(
                        'this tool cannot treat well python version constraint "*".\n' 'Specify more concrete, e.g. "^3.9".'
                    )
                )
            else:
                return Err(InvalidPythonVersionError(f"cannot understand: {spec}"))

I'll turn off black temporary.

kenoss commented 3 years ago

Put my eyes on https://github.com/psf/black/issues/569 .