Skip to content

Commit 87ca0e1

Browse files
authored
DOC: Move code_style.rst docs to other sections & a check (#46724)
1 parent 03ecf60 commit 87ca0e1

File tree

6 files changed

+95
-35
lines changed

6 files changed

+95
-35
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ repos:
176176
files: ^pandas/core/
177177
exclude: ^pandas/core/api\.py$
178178
types: [python]
179+
- id: use-io-common-urlopen
180+
name: Use pandas.io.common.urlopen instead of urllib.request.urlopen
181+
language: python
182+
entry: python scripts/use_io_common_urlopen.py
183+
files: ^pandas/
184+
exclude: ^pandas/tests/
185+
types: [python]
179186
- id: no-bool-in-core-generic
180187
name: Use bool_t instead of bool in pandas/core/generic.py
181188
entry: python scripts/no_bool_in_generic.py

doc/source/development/code_style.rst

Lines changed: 0 additions & 31 deletions
This file was deleted.

doc/source/development/contributing_codebase.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ In addition to ``./ci/code_checks.sh``, some extra checks are run by
3737
``pre-commit`` - see :ref:`here <contributing.pre-commit>` for how to
3838
run them.
3939

40-
Additional standards are outlined on the :ref:`pandas code style guide <code_style>`.
41-
4240
.. _contributing.pre-commit:
4341

4442
Pre-commit
4543
----------
4644

4745
Additionally, :ref:`Continuous Integration <contributing.ci>` will run code formatting checks
48-
like ``black``, ``flake8``, ``isort``, and ``cpplint`` and more using `pre-commit hooks <https://pre-commit.com/>`_
46+
like ``black``, ``flake8`` (including a `pandas-dev-flaker <https://github.com/pandas-dev/pandas-dev-flaker>`_ plugin),
47+
``isort``, and ``cpplint`` and more using `pre-commit hooks <https://pre-commit.com/>`_
4948
Any warnings from these checks will cause the :ref:`Continuous Integration <contributing.ci>` to fail; therefore,
5049
it is helpful to run the check yourself before submitting code. This
5150
can be done by installing ``pre-commit``::

doc/source/development/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Development
1616
contributing_environment
1717
contributing_documentation
1818
contributing_codebase
19-
code_style
2019
maintaining
2120
internals
2221
test_writing
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
from scripts.use_io_common_urlopen import use_io_common_urlopen
4+
5+
PATH = "t.py"
6+
7+
8+
def test_inconsistent_usage(capsys):
9+
content = "from urllib.request import urlopen"
10+
result_msg = (
11+
"t.py:1:0: Don't use urllib.request.urlopen, "
12+
"use pandas.io.common.urlopen instead\n"
13+
)
14+
with pytest.raises(SystemExit, match=None):
15+
use_io_common_urlopen(content, PATH)
16+
expected_msg, _ = capsys.readouterr()
17+
assert result_msg == expected_msg
18+
19+
20+
def test_consistent_usage():
21+
# should not raise
22+
content = "from pandas.io.common import urlopen"
23+
use_io_common_urlopen(content, PATH)

scripts/use_io_common_urlopen.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Check that pandas/core imports pandas.array as pd_array.
3+
4+
This makes it easier to grep for usage of pandas array.
5+
6+
This is meant to be run as a pre-commit hook - to run it manually, you can do:
7+
8+
pre-commit run use-io-common-urlopen --all-files
9+
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import argparse
15+
import ast
16+
import sys
17+
from typing import Sequence
18+
19+
ERROR_MESSAGE = (
20+
"{path}:{lineno}:{col_offset}: "
21+
"Don't use urllib.request.urlopen, use pandas.io.common.urlopen instead\n"
22+
)
23+
24+
25+
class Visitor(ast.NodeVisitor):
26+
def __init__(self, path: str) -> None:
27+
self.path = path
28+
29+
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
30+
# Check that pandas.io.common.urlopen is used instead of
31+
# urllib.request.urlopen
32+
if (
33+
node.module is not None
34+
and node.module.startswith("urllib.request")
35+
and any(i.name == "urlopen" for i in node.names)
36+
):
37+
msg = ERROR_MESSAGE.format(
38+
path=self.path, lineno=node.lineno, col_offset=node.col_offset
39+
)
40+
sys.stdout.write(msg)
41+
sys.exit(1)
42+
super().generic_visit(node)
43+
44+
45+
def use_io_common_urlopen(content: str, path: str) -> None:
46+
tree = ast.parse(content)
47+
visitor = Visitor(path)
48+
visitor.visit(tree)
49+
50+
51+
def main(argv: Sequence[str] | None = None) -> None:
52+
parser = argparse.ArgumentParser()
53+
parser.add_argument("paths", nargs="*")
54+
args = parser.parse_args(argv)
55+
56+
for path in args.paths:
57+
with open(path, encoding="utf-8") as fd:
58+
content = fd.read()
59+
use_io_common_urlopen(content, path)
60+
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)