Skip to content

Commit

Permalink
Add pre-commit (scrapy#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
VMRuiz authored Apr 21, 2023
1 parent 54fc3cc commit 7c98250
Show file tree
Hide file tree
Showing 24 changed files with 616 additions and 522 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Apply black format
627f3bd9ea5210f40dbd5697eff9351bb5af019c
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ on:
- pull_request
- push
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: "3.11"
- uses: pre-commit/action@v3.0.0

tests:
runs-on: ubuntu-latest
strategy:
Expand Down
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
default_language_version:
python: python3.10
repos:
- hooks:
- id: black
language_version: python3
repo: https://github.com/ambv/black
rev: 23.3.0
- hooks:
- id: isort
language_version: python3
repo: https://github.com/PyCQA/isort
rev: 5.12.0
- hooks:
- id: flake8
language_version: python3
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- flake8-debugger
- flake8-docstrings
- flake8-string-format
repo: https://github.com/pycqa/flake8
rev: 6.0.0
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ All contributions are welcome!
* File an `issue here <https://github.com/scrapy/itemloaders/issues>`_, if there isn't one yet
* Fork this repository
* Create a branch to work on your changes
* Run `pre-commit install` to install pre-commit hooks
* Push your local branch and submit a Pull Request
1 change: 0 additions & 1 deletion docs/_ext/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

22 changes: 17 additions & 5 deletions docs/_ext/github.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
from typing import Optional

from docutils import nodes
from docutils.parsers.rst.roles import set_classes


def setup(app):
app.add_role('gh', github_role)
app.add_role("gh", github_role)


def github_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
def github_role(
name,
rawtext,
text,
lineno,
inliner,
options: Optional[dict] = None,
content: Optional[list] = None,
):
options = options or {}
content = content or []
if text.isdigit():
display_text = f'#{text}'
url = f'https://github.com/scrapy/itemloaders/issues/{text}'
display_text = f"#{text}"
url = f"https://github.com/scrapy/itemloaders/issues/{text}"
else:
short_commit = text[:7]
display_text = short_commit
url = f'https://github.com/scrapy/itemloaders/commit/{short_commit}'
url = f"https://github.com/scrapy/itemloaders/commit/{short_commit}"

set_classes(options)
node = nodes.reference(rawtext, display_text, refuri=url, **options)
Expand Down
110 changes: 56 additions & 54 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from datetime import datetime
from os import path

import sphinx_rtd_theme

# If your extensions are in another directory, add it here. If the directory
# is relative to the documentation root, use os.path.abspath to make it
# absolute, like shown here.
Expand All @@ -27,90 +29,90 @@
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
'_ext.github',
'sphinx.ext.autodoc',
'sphinx.ext.coverage',
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
"_ext.github",
"sphinx.ext.autodoc",
"sphinx.ext.coverage",
"sphinx.ext.intersphinx",
"sphinx.ext.viewcode",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix of source filenames.
source_suffix = '.rst'
source_suffix = ".rst"

# The encoding of source files.
#source_encoding = 'utf-8'
# source_encoding = 'utf-8'

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# General information about the project.
project = 'itemloaders'
copyright = '2020–{}, Zyte Group Ltd'.format(datetime.now().year)
project = "itemloaders"
copyright = "2020–{}, Zyte Group Ltd".format(datetime.now().year)

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = ''
release = ''
version = ""
release = ""

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
language = 'en'
language = "en"

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# today_fmt = '%B %d, %Y'

# List of documents that shouldn't be included in the build.
#unused_docs = []
# unused_docs = []

exclude_patterns = ['build']
exclude_patterns = ["build"]

# List of directories, relative to source directory, that shouldn't be searched
# for source files.
exclude_trees = ['.build']
exclude_trees = [".build"]

# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None
# default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"


# Options for HTML output
# -----------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
# Add path to the RTD explicitly to robustify builds (otherwise might
# fail in a clean Debian build env)
import sphinx_rtd_theme

html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]


Expand All @@ -121,19 +123,19 @@

# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# html_title = None

# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand All @@ -142,92 +144,92 @@

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
html_last_updated_fmt = '%b %d, %Y'
html_last_updated_fmt = "%b %d, %Y"

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# html_additional_pages = {}

# If false, no module index is generated.
#html_use_modindex = True
# html_use_modindex = True

# If false, no index is generated.
#html_use_index = True
# html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False
# html_split_index = False

# If true, the reST sources are included in the HTML build as _sources/<name>.
html_copy_source = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# html_use_opensearch = ''

# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = ''
# html_file_suffix = ''

# Output file base name for HTML help builder.
htmlhelp_basename = 'itemloadersdoc'
htmlhelp_basename = "itemloadersdoc"


# Options for LaTeX output
# ------------------------

# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'
# latex_paper_size = 'letter'

# The font size ('10pt', '11pt' or '12pt').
#latex_font_size = '10pt'
# latex_font_size = '10pt'

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, document class [howto/manual]).
latex_documents = [
('index', 'itemloaders.tex', 'itemloaders Documentation', 'Zyte', 'manual'),
("index", "itemloaders.tex", "itemloaders Documentation", "Zyte", "manual"),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# latex_use_parts = False

# Additional stuff for the LaTeX preamble.
#latex_preamble = ''
# latex_preamble = ''

# Documents to append as an appendix to all manuals.
#latex_appendices = []
# latex_appendices = []

# If false, no module index is generated.
#latex_use_modindex = True
# latex_use_modindex = True


# autodocs


def setup(app):
app.connect('autodoc-skip-member', maybe_skip_member)
app.connect("autodoc-skip-member", maybe_skip_member)


def maybe_skip_member(app, what, name, obj, skip, options):
if not skip:
# autodocs was generating a text "alias of" for the following members
# https://github.com/sphinx-doc/sphinx/issues/4422
return name in {'default_item_class', 'default_selector_class'}
return name in {"default_item_class", "default_selector_class"}
return skip


nitpicky = True

intersphinx_mapping = {
'parsel': ('https://parsel.readthedocs.io/en/stable/', None),
'python': ('https://docs.python.org/3', None),
'scrapy': ('https://docs.scrapy.org/en/latest/', None),
'w3lib': ('https://w3lib.readthedocs.io/en/latest', None),
"parsel": ("https://parsel.readthedocs.io/en/stable/", None),
"python": ("https://docs.python.org/3", None),
"scrapy": ("https://docs.scrapy.org/en/latest/", None),
"w3lib": ("https://w3lib.readthedocs.io/en/latest", None),
}
6 changes: 3 additions & 3 deletions itemloaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def get_value(self, value, *processors, re=None, **kw):
raise ValueError(
"Error with processor %s value=%r error='%s: %s'"
% (_proc.__class__.__name__, value, type(e).__name__, str(e))
)
) from e
return value

def load_item(self):
Expand Down Expand Up @@ -276,7 +276,7 @@ def get_output_value(self, field_name):
raise ValueError(
"Error with output processor: field=%r value=%r error='%s: %s'"
% (field_name, value, type(e).__name__, str(e))
)
) from e

def get_collected_values(self, field_name):
"""Return the collected values for the given field."""
Expand Down Expand Up @@ -319,7 +319,7 @@ def _process_input_value(self, field_name, value):
type(e).__name__,
str(e),
)
)
) from e

def _check_selector_method(self):
if self.selector is None:
Expand Down
3 changes: 2 additions & 1 deletion itemloaders/common.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Common functions used in Item Loaders code"""

from functools import partial

from itemloaders.utils import get_func_args


def wrap_loader_context(function, context):
"""Wrap functions that receive loader_context to contain the context
"pre-loaded" and expose a interface that receives only one argument
"""
if 'loader_context' in get_func_args(function):
if "loader_context" in get_func_args(function):
return partial(function, loader_context=context)
else:
return function
Loading

0 comments on commit 7c98250

Please sign in to comment.