Skip to content

DOC: Move code_style.rst docs to other sections & a check #46724

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

Merged
merged 5 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ repos:
files: ^pandas/core/
exclude: ^pandas/core/api\.py$
types: [python]
- id: use-io-common-urlopen
name: Use pandas.io.common.urlopen instead of urllib.request.urlopen
language: python
entry: python scripts/use_io_common_urlopen.py
files: ^pandas/
exclude: ^pandas/tests/
types: [python]
- id: no-bool-in-core-generic
name: Use bool_t instead of bool in pandas/core/generic.py
entry: python scripts/no_bool_in_generic.py
Expand Down
31 changes: 0 additions & 31 deletions doc/source/development/code_style.rst

This file was deleted.

5 changes: 2 additions & 3 deletions doc/source/development/contributing_codebase.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ In addition to ``./ci/code_checks.sh``, some extra checks are run by
``pre-commit`` - see :ref:`here <contributing.pre-commit>` for how to
run them.

Additional standards are outlined on the :ref:`pandas code style guide <code_style>`.

.. _contributing.pre-commit:

Pre-commit
----------

Additionally, :ref:`Continuous Integration <contributing.ci>` will run code formatting checks
like ``black``, ``flake8``, ``isort``, and ``cpplint`` and more using `pre-commit hooks <https://pre-commit.com/>`_
like ``black``, ``flake8`` (including a `pandas-dev-flaker <https://github.com/pandas-dev/pandas-dev-flaker>`_ plugin),
``isort``, and ``cpplint`` and more using `pre-commit hooks <https://pre-commit.com/>`_
Any warnings from these checks will cause the :ref:`Continuous Integration <contributing.ci>` to fail; therefore,
it is helpful to run the check yourself before submitting code. This
can be done by installing ``pre-commit``::
Expand Down
1 change: 0 additions & 1 deletion doc/source/development/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Development
contributing_environment
contributing_documentation
contributing_codebase
code_style
maintaining
internals
test_writing
Expand Down
23 changes: 23 additions & 0 deletions scripts/tests/test_use_io_common_urlopen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from scripts.use_io_common_urlopen import use_io_common_urlopen

PATH = "t.py"


def test_inconsistent_usage(capsys):
content = "from urllib.request import urlopen"
result_msg = (
"t.py:1:0: Don't use urllib.request.urlopen, "
"use pandas.io.common.urlopen instead\n"
)
with pytest.raises(SystemExit, match=None):
use_io_common_urlopen(content, PATH)
expected_msg, _ = capsys.readouterr()
assert result_msg == expected_msg


def test_consistent_usage():
# should not raise
content = "from pandas.io.common import urlopen"
use_io_common_urlopen(content, PATH)
63 changes: 63 additions & 0 deletions scripts/use_io_common_urlopen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Check that pandas/core imports pandas.array as pd_array.

This makes it easier to grep for usage of pandas array.

This is meant to be run as a pre-commit hook - to run it manually, you can do:

pre-commit run use-io-common-urlopen --all-files

"""

from __future__ import annotations

import argparse
import ast
import sys
from typing import Sequence

ERROR_MESSAGE = (
"{path}:{lineno}:{col_offset}: "
"Don't use urllib.request.urlopen, use pandas.io.common.urlopen instead\n"
)


class Visitor(ast.NodeVisitor):
def __init__(self, path: str) -> None:
self.path = path

def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
# Check that pandas.io.common.urlopen is used instead of
# urllib.request.urlopen
if (
node.module is not None
and node.module.startswith("urllib.request")
and any(i.name == "urlopen" for i in node.names)
):
msg = ERROR_MESSAGE.format(
path=self.path, lineno=node.lineno, col_offset=node.col_offset
)
sys.stdout.write(msg)
sys.exit(1)
super().generic_visit(node)


def use_io_common_urlopen(content: str, path: str) -> None:
tree = ast.parse(content)
visitor = Visitor(path)
visitor.visit(tree)


def main(argv: Sequence[str] | None = None) -> None:
parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="*")
args = parser.parse_args(argv)

for path in args.paths:
with open(path, encoding="utf-8") as fd:
content = fd.read()
use_io_common_urlopen(content, path)


if __name__ == "__main__":
main()