pawamoy / copier-poetry

Copier template for Python projects managed by Poetry.
https://pawamoy.github.io/copier-poetry/
ISC License
80 stars 14 forks source link

type error on macros #22

Closed rachmadaniHaryono closed 2 years ago

rachmadaniHaryono commented 2 years ago

when i remove code of conduct i get error on macros.py

src/iqdb_tagger/main.pymkdocs build -s✗ Building documentation (1)
  > mkdocs build -s
  INFO     -  [macros] - Macros arguments: {'module_name': 'docs/macros',
              'modules': [], 'include_dir': '', 'include_yaml': [],
              'j2_block_start_string': '', 'j2_block_end_string': '',
              'j2_variable_start_string': '', 'j2_variable_end_string': '',
              'verbose': False}
  INFO     -  [macros] - Found local Python module 'docs/macros' in:
              /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger
  INFO     -  [macros] - Found external Python module 'docs/macros' in:
              /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger
  INFO     -  [macros] - Extra filters (module): ['pretty']
  INFO     -  Cleaning site directory
  INFO     -  Building documentation to directory:
              /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/site
  WARNING  -  A relative path to 'code_of_conduct.md' is included in the 'nav'
              configuration, which is not found in the documentation files
  Package(s) not found: astunparse, atomicwrites, cached-property, pywin32-ctypes
  INFO     -  [macros] - ERROR # _Macro Rendering Error_

              **TypeError**: tuple indices must be integers or slices, not str
          Traceback (most recent call last):
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/mkdocs_macros/plugin.py",
          line 434, in render
              return md_template.render(**page_variables)
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py",
          line 1090, in render
              self.environment.handle_exception()
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py",
          line 832, in handle_exception
              reraise(*rewrite_traceback_stack(source=source))
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/_compat.py",
          line 28, in reraise
              raise value.with_traceback(tb)
            File "<template>", line 1, in top-level template code
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 81, in credits
              return get_credits()
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 63, in get_credits
              template_data = get_credits_data()
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 35, in get_credits_data
              pkg = {_: pkg[_] for _ in ("name", "home-page")}  # type: ignore
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 35, in <dictcomp>
              pkg = {_: pkg[_] for _ in ("name", "home-page")}  # type: ignore
          TypeError: tuple indices must be integers or slices, not str

          ```

Aborted with 1 warnings in strict mode! WPS332 Found walrus operator> poetry run mkdocs build -s INFO - [macros] - Macros arguments: {'module_name': 'docs/macros', 'modules': [], 'include_dir': '', 'include_yaml': [], 'j2_block_start_string': '', 'j2_block_end_string': '', 'j2_variable_start_string': '', 'j2_variable_end_string': '', 'verbose': False} INFO - [macros] - Found local Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Found external Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Extra filters (module): ['pretty'] INFO - Cleaning site directory INFO - Building documentation to directory: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/site WARNING - A relative path to 'code_of_conduct.md' is included in the 'nav' configuration, which is not found in the documentation files Package(s) not found: astunparse, atomicwrites, cached-property, pywin32-ctypes WARNING:root:tuple indices must be integers or slices, not str Traceback (most recent call last): File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 37, in get_creditsdata pkg = {: pkg[] for in ("name", "home-page")} # type: ignore File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdbtagger/docs/macros.py", line 37, in pkg = {: pkg[] for in ("name", "home-page")} # type: ignore TypeError: tuple indices must be integers or slices, not str INFO - [macros] - ERROR # Macro Rendering Error

        **KeyError**: 'name'

        ```
        Traceback (most recent call last):
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/mkdocs_macros/plugin.py", line 434, in render
            return md_template.render(**page_variables)
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py", line 1090, in render
            self.environment.handle_exception()
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
            reraise(*rewrite_traceback_stack(source=source))
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
            raise value.with_traceback(tb)
          File "<template>", line 1, in top-level template code
          File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 86, in credits
            return get_credits()
          File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 68, in get_credits
            template_data = get_credits_data()
          File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 41, in get_credits_data
            packages[pkg["name"].lower()] = pkg  # type: ignore
        KeyError: 'name'

        ```

Aborted with 1 warnings in strict mode! INFO - [macros] - Macros arguments: {'module_name': 'docs/macros', 'modules': [], 'include_dir': '', 'include_yaml': [], 'j2_block_start_string': '', 'j2_block_end_string': '', 'j2_variable_start_string': '', 'j2_variable_end_string': '', 'verbose': False} INFO - [macros] - Found local Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Found external Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Extra filters (module): ['pretty'] INFO - Cleaning site directory INFO - Building documentation to directory: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/site WARNING - A relative path to 'code_of_conduct.md' is included in the 'nav' configuration, which is not found in the documentation files Package(s) not found: astunparse, atomicwrites, cached-property, pywin32-ctypes

Aborted with 1 warnings in strict mode!

make: *** [Makefile:45: check] Error 1

i tried to by pass that but got another error
```python
src/iqdb_tagger/main.pymkdocs build -s✗ Building documentation (1)
  > mkdocs build -s
  INFO     -  [macros] - Macros arguments: {'module_name': 'docs/macros',
              'modules': [], 'include_dir': '', 'include_yaml': [],
              'j2_block_start_string': '', 'j2_block_end_string': '',
              'j2_variable_start_string': '', 'j2_variable_end_string': '',
              'verbose': False}
  INFO     -  [macros] - Found local Python module 'docs/macros' in:
              /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger
  INFO     -  [macros] - Found external Python module 'docs/macros' in:
              /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger
  INFO     -  [macros] - Extra filters (module): ['pretty']
  INFO     -  Cleaning site directory
  INFO     -  Building documentation to directory:
              /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/site
  WARNING  -  A relative path to 'code_of_conduct.md' is included in the 'nav'
              configuration, which is not found in the documentation files
  Package(s) not found: astunparse, atomicwrites, cached-property, pywin32-ctypes
  INFO     -  [macros] - ERROR # _Macro Rendering Error_

              **TypeError**: tuple indices must be integers or slices, not str
          Traceback (most recent call last):
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/mkdocs_macros/plugin.py",
          line 434, in render
              return md_template.render(**page_variables)
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py",
          line 1090, in render
              self.environment.handle_exception()
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py",
          line 832, in handle_exception
              reraise(*rewrite_traceback_stack(source=source))
            File
          "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/_compat.py",
          line 28, in reraise
              raise value.with_traceback(tb)
            File "<template>", line 1, in top-level template code
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 81, in credits
              return get_credits()
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 63, in get_credits
              template_data = get_credits_data()
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 35, in get_credits_data
              pkg = {_: pkg[_] for _ in ("name", "home-page")}  # type: ignore
            File
          "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py",
          line 35, in <dictcomp>
              pkg = {_: pkg[_] for _ in ("name", "home-page")}  # type: ignore
          TypeError: tuple indices must be integers or slices, not str

          ```

Aborted with 1 warnings in strict mode! WPS332 Found walrus operator> poetry run mkdocs build -s INFO - [macros] - Macros arguments: {'module_name': 'docs/macros', 'modules': [], 'include_dir': '', 'include_yaml': [], 'j2_block_start_string': '', 'j2_block_end_string': '', 'j2_variable_start_string': '', 'j2_variable_end_string': '', 'verbose': False} INFO - [macros] - Found local Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Found external Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Extra filters (module): ['pretty'] INFO - Cleaning site directory INFO - Building documentation to directory: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/site WARNING - A relative path to 'code_of_conduct.md' is included in the 'nav' configuration, which is not found in the documentation files Package(s) not found: astunparse, atomicwrites, cached-property, pywin32-ctypes WARNING:root:tuple indices must be integers or slices, not str Traceback (most recent call last): File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 37, in get_creditsdata pkg = {: pkg[] for in ("name", "home-page")} # type: ignore File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdbtagger/docs/macros.py", line 37, in pkg = {: pkg[] for in ("name", "home-page")} # type: ignore TypeError: tuple indices must be integers or slices, not str INFO - [macros] - ERROR # Macro Rendering Error

        **KeyError**: 'name'

        ```
        Traceback (most recent call last):
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/mkdocs_macros/plugin.py", line 434, in render
            return md_template.render(**page_variables)
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py", line 1090, in render
            self.environment.handle_exception()
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
            reraise(*rewrite_traceback_stack(source=source))
          File "/home/r3r/.cache/pypoetry/virtualenvs/iqdb-tagger-C28AsU-2-py3.9/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
            raise value.with_traceback(tb)
          File "<template>", line 1, in top-level template code
          File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 86, in credits
            return get_credits()
          File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 68, in get_credits
            template_data = get_credits_data()
          File "/mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/docs/macros.py", line 41, in get_credits_data
            packages[pkg["name"].lower()] = pkg  # type: ignore
        KeyError: 'name'

        ```

Aborted with 1 warnings in strict mode! INFO - [macros] - Macros arguments: {'module_name': 'docs/macros', 'modules': [], 'include_dir': '', 'include_yaml': [], 'j2_block_start_string': '', 'j2_block_end_string': '', 'j2_variable_start_string': '', 'j2_variable_end_string': '', 'verbose': False} INFO - [macros] - Found local Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Found external Python module 'docs/macros' in: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger INFO - [macros] - Extra filters (module): ['pretty'] INFO - Cleaning site directory INFO - Building documentation to directory: /mnt/ac54dceb-73a5-4f94-b52c-cb7a426c0f29/Documents/iqdb_tagger/site WARNING - A relative path to 'code_of_conduct.md' is included in the 'nav' configuration, which is not found in the documentation files Package(s) not found: astunparse, atomicwrites, cached-property, pywin32-ctypes

Aborted with 1 warnings in strict mode!

make: *** [Makefile:45: check] Error 1

this is fix for python3.8
```diff
Author: Rachmadani Haryono <foreturiga@gmail.com>
Date:   Tue Dec 14 07:45:16 2021 +0800

    fix(macros): type error

diff --git a/project/docs/macros.py b/project/docs/macros.py
index 72262ad..2ccf826 100644
--- a/project/docs/macros.py
+++ b/project/docs/macros.py
@@ -4,6 +4,7 @@ import functools
 from itertools import chain
 from pathlib import Path

+import logging
 import httpx
 import toml
 from jinja2 import StrictUndefined
@@ -32,8 +33,13 @@ def get_credits_data() -> dict:

     packages = {}
     for pkg in search_packages_info(list(dependencies)):
-        pkg = {_: pkg[_] for _ in ("name", "home-page")}  # type: ignore
-        packages[pkg["name"].lower()] = pkg  # type: ignore
+        try:
+            pkg_data = {_: pkg[_] for _ in ("name", "home-page")}  # type: ignore
+        except TypeError as err:
+            logging.debug(err, exc_info=True)
+            pkg_data = {}
+        if pkg_name := pkg_data.get("name"):
+            packages[pkg_name.lower()] = pkg_data  # type: ignore

     # all packages might not be credited,
     # like the ones that are now part of the standard library
pawamoy commented 2 years ago

Take a look at a more recent version in the PDM version of the template: https://github.com/pawamoy/copier-pdm/blob/master/project/docs/gen_credits.py.jinja

Feel free to send a PR :slightly_smiling_face: