Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pydoclint as pre-commit hook #20

Closed
liebsc21 opened this issue Jun 2, 2023 · 8 comments · Fixed by #21
Closed

Use pydoclint as pre-commit hook #20

liebsc21 opened this issue Jun 2, 2023 · 8 comments · Fixed by #21

Comments

@liebsc21
Copy link

liebsc21 commented Jun 2, 2023

I like to use pydoclint as pre-commit hook.

It works perfectly well when running it from the CL pydoclint --config=pyproject.toml src tests rest_api.
But running the hook defined in pre-commit-config.yaml:

  - repo: local
    hooks:
      - id: pydoclint
        name: pydoclint
        entry: pydoclint --config=pyproject.toml
        language: system
        files: ^(src|tests|rest_api)/.*\.py$
        always_run: true
        stages: [pre-commit, pre-push, manual]

with pre-commit run --all-files --hook-stage manual pydoclint results in

pydoclint................................................................Failed
- hook id: pydoclint
- exit code: 1

Loading config from user-specified .toml file: pyproject.toml
Found options defined in pyproject.toml:
{'style': 'google', 'allow_init_docstring': True, 'check_type_hint': False}
Skipping files that match this pattern: \.git|\.tox
rest_api\tool_1\views.py
rest_api\tool_1\apps.py
rest_api\config\urls.py
rest_api\tool_1\urls.py
Traceback (most recent call last):
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\Scripts\pydoclint.EXE\__main__.py", line 7, in <module>
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\pydoclint\main.py", line 219, in main
    click.echo(click.style('\U0001f389 No violations \U0001f389', fg='green', bold=True))
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\utils.py", line 299, in echo
    file.write(out)  # type: ignore
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f389' in position 0: character maps to <undefined>
Loading config from user-specified .toml file: pyproject.toml
Found options defined in pyproject.toml:
{'style': 'google', 'allow_init_docstring': True, 'check_type_hint': False}
Skipping files that match this pattern: \.git|\.tox
rest_api\tool_1\tests.py
rest_api\tool_1\models.py
rest_api\config\__init__.py
rest_api\config\asgi.py
Traceback (most recent call last):
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\Scripts\pydoclint.EXE\__main__.py", line 7, in <module>
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\pydoclint\main.py", line 219, in main
    click.echo(click.style('\U0001f389 No violations \U0001f389', fg='green', bold=True))
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\utils.py", line 299, in echo
    file.write(out)  # type: ignore
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f389' in position 0: character maps to <undefined>
Loading config from user-specified .toml file: pyproject.toml
Found options defined in pyproject.toml:
{'style': 'google', 'allow_init_docstring': True, 'check_type_hint': False}
Skipping files that match this pattern: \.git|\.tox
src\new_sunfire_python_package\tool_1.py
rest_api\config\helpers.py
rest_api\tool_1\admin.py
rest_api\config\settings.py
Traceback (most recent call last):
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\Scripts\pydoclint.EXE\__main__.py", line 7, in <module>
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\pydoclint\main.py", line 219, in main
    click.echo(click.style('\U0001f389 No violations \U0001f389', fg='green', bold=True))
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\utils.py", line 299, in echo
    file.write(out)  # type: ignore
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f389' in position 0: character maps to <undefined>
Loading config from user-specified .toml file: pyproject.toml
Found options defined in pyproject.toml:
{'style': 'google', 'allow_init_docstring': True, 'check_type_hint': False}
Skipping files that match this pattern: \.git|\.tox
rest_api\tool_1\__init__.py
tests\tool_1\test_tool_1.py
rest_api\config\wsgi.py
rest_api\tool_1\migrations\__init__.py
Traceback (most recent call last):
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\Scripts\pydoclint.EXE\__main__.py", line 7, in <module>
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\pydoclint\main.py", line 219, in main
    click.echo(click.style('\U0001f389 No violations \U0001f389', fg='green', bold=True))
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\utils.py", line 299, in echo
    file.write(out)  # type: ignore
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f389' in position 0: character maps to <undefined>
Loading config from user-specified .toml file: pyproject.toml
Found options defined in pyproject.toml:
{'style': 'google', 'allow_init_docstring': True, 'check_type_hint': False}
Skipping files that match this pattern: \.git|\.tox
rest_api\manage.py
src\new_sunfire_python_package\__init__.py
Traceback (most recent call last):
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\Scripts\pydoclint.EXE\__main__.py", line 7, in <module>
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\pydoclint\main.py", line 219, in main
    click.echo(click.style('\U0001f389 No violations \U0001f389', fg='green', bold=True))
  File "C:\Users\liebschs\MyFiles\playground\cookiecutter-playground\new-sunfire-python-package\venv\lib\site-packages\click\utils.py", line 299, in echo
    file.write(out)  # type: ignore
  File "C:\Users\liebschs\Programs\WinPython64-3.10.9\WPy64-31090\python-3.10.9.amd64\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f389' in position 0: character maps to <undefined>

It seems that it lints all files it should, but for some a UnicodeEncodeError is raised. Any ideas?

@jsh9
Copy link
Owner

jsh9 commented Jun 2, 2023

Interesting...

This is because pydoclint tries to print an emoji (🎉) in the stdout:

click.echo(click.style('🎉 No violations 🎉', fg='green', bold=True))

So somehow, either pre-commit or Windows decided to not render emojis.

Btw, I've never tried to use pydoclint as a pre-commit hook yet (there's an ongoing PR to support it: #18).

Let me try it out on my computer (macOS). I'll get back to you.

@jsh9
Copy link
Owner

jsh9 commented Jun 2, 2023

I just confirmed that on macOS, "🎉" can be rendered correctly in the terminal with pre-commit.

I guess I'll need to create a non-emoji stdout for Windows then.

@liebsc21
Copy link
Author

liebsc21 commented Jun 2, 2023

That'd be great.
Is the emoji only printed when using pre-commit?

@s-weigand
Copy link
Contributor

Not sure what exactly but black does something that makes it work
https://github.com/psf/black/blob/3aad6e385bfbd4348b2e13695cb6741806951160/src/black/__init__.py#LL614C69-L614C69

Using the pre-commit config from @liebsc21 I could reproduce the error with pydoclint

But if I do the same with black it manages to print things w/o crashing

  - repo: local
    hooks:
      - id: pydoclint
        name: pydoclint
        entry: pydoclint --config=pyproject.toml
        language: system
        files: ^(src|tests|rest_api)/.*\.py$
        always_run: true
      - id: black-local
        name: black-local
        entry: black
        language: system
        files: ^(src|tests|rest_api)/.*\.py$
        always_run: true
>pre-commit run black-local --all -v
black-local..............................................................Passed
- hook id: black-local
- duration: 0.6s

All done! \u2728 \U0001f370 \u2728
4 files left unchanged.
All done! \u2728 \U0001f370 \u2728
4 files left unchanged.
All done! \u2728 \U0001f370 \u2728
4 files left unchanged.

@s-weigand
Copy link
Contributor

s-weigand commented Jun 2, 2023

Ok found it, looks like windows, click or pre-cmmit is handling encoding in stdout differently than in stderr.
So changing the output to stderr (as black does it) fixes the issue

-             click.echo(click.style("🎉 No violations 🎉", fg="green", bold=True))
+             click.echo(click.style("🎉 No violations 🎉", fg="green", bold=True), err=True)

@jsh9
Copy link
Owner

jsh9 commented Jun 3, 2023

Hi @s-weigand , thanks for finding out how Black bypasses this issue!

That explains why when I ran Black and got "All done! ✨ 🍰 ✨", the texts were always red, as if it was an error.

I think Black's hacky way here can lead to confusions -- both for the users and for the developers. That's why I made this suggestion in the PR here.

@jsh9 jsh9 closed this as completed in #21 Jun 5, 2023
@liebsc21
Copy link
Author

liebsc21 commented Jun 6, 2023

Thanks again for the quick fix.
Do you know when you will release this?

@jsh9
Copy link
Owner

jsh9 commented Jun 6, 2023

Hi @liebsc21 , I just released 0.0.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants