Skip to content

Cleanup/normalize coding style #359

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 4 commits into from
Jul 14, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/linuxbrew.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: linuxbrew
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress: ${{ github.ref_name != 'master' }}
jobs:
linuxbrew:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macosx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: macOS
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress: ${{ github.ref_name != 'master' }}
jobs:
macosx:
runs-on: macos-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/manylinux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: manylinux
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress: ${{ github.ref_name != 'master' }}
jobs:
manylinux:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sdist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sdist
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress: ${{ github.ref_name != 'master' }}
jobs:
sdist:
# Avoid Ubuntu 24.04 in sdist workflows, because it contains libxmlsec1-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress: ${{ github.ref_name != 'master' }}

permissions: {}

Expand Down
84 changes: 39 additions & 45 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 25.1.0
hooks:
- id: black
types: []
files: ^.*.pyi?$
exclude: ^doc/
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: no-commit-to-branch
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-ast
- id: check-merge-conflict
- id: check-json
- id: detect-private-key
exclude: ^.*/rsakey.pem$
- id: mixed-line-ending
- id: pretty-format-json
args: [--autofix]
- repo: https://github.com/PyCQA/flake8
rev: 7.3.0
hooks:
- id: flake8
exclude: ^setup.py$
additional_dependencies: [flake8-docstrings, flake8-bugbear, flake8-logging-format, flake8-builtins, flake8-eradicate, flake8-fixme, pep8-naming, flake8-pep3101, flake8-annotations-complexity,flake8-pyi]
- repo: https://github.com/PyCQA/isort
rev: 6.0.1
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.16.1
hooks:
- id: mypy
exclude: (setup.py|tests/.*.py|doc/.*)
types: []
files: ^.*.pyi?$
additional_dependencies: [lxml-stubs,types-docutils]
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: rst-backticks
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.3
hooks:
- id: ruff
args: ["--fix"]
types: [python]
- id: ruff-format
types: [python]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: no-commit-to-branch
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-ast
- id: check-merge-conflict
- id: check-json
- id: detect-private-key
exclude: ^.*/rsakey.pem$
- id: mixed-line-ending
- id: pretty-format-json
args: [--autofix]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.16.1
hooks:
- id: mypy
exclude: (setup.py|tests/.*.py|doc/.*)
types: []
files: ^.*.pyi?$
additional_dependencies: [lxml-stubs, types-docutils]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: rst-backticks
25 changes: 12 additions & 13 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
source_suffix = '.rst'
master_doc = 'index'

project = u'python-xmlsec'
copyright = u'2020, Oleg Hoefling <oleg.hoefling@gmail.com>' # noqa: A001
author = u'Bulat Gaifullin <gaifullinbf@gmail.com>'
project = 'python-xmlsec'
copyright = '2020, Oleg Hoefling <oleg.hoefling@gmail.com>'
author = 'Bulat Gaifullin <gaifullinbf@gmail.com>'
release = importlib.metadata.version('xmlsec')
parsed: Version = parse(release)
version = '{}.{}'.format(parsed.major, parsed.minor)
version = f'{parsed.major}.{parsed.minor}'

exclude_patterns: list[str] = []
pygments_style = 'sphinx'
Expand All @@ -39,19 +39,19 @@
(
master_doc,
'python-xmlsec.tex',
u'python-xmlsec Documentation',
u'Bulat Gaifullin \\textless{}gaifullinbf@gmail.com\\textgreater{}',
'python-xmlsec Documentation',
'Bulat Gaifullin \\textless{}gaifullinbf@gmail.com\\textgreater{}',
'manual',
)
]

man_pages = [(master_doc, 'python-xmlsec', u'python-xmlsec Documentation', [author], 1)]
man_pages = [(master_doc, 'python-xmlsec', 'python-xmlsec Documentation', [author], 1)]

texinfo_documents = [
(
master_doc,
'python-xmlsec',
u'python-xmlsec Documentation',
'python-xmlsec Documentation',
author,
'python-xmlsec',
'One line description of project.',
Expand All @@ -63,10 +63,10 @@
autodoc_docstring_signature = True


rst_prolog = '''
rst_prolog = """
.. role:: xml(code)
:language: xml
'''
"""

# LXML crossref'ing stuff:
# LXML doesn't have an intersphinx docs,
Expand All @@ -75,8 +75,7 @@


def lxml_element_doc_reference(app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: Text) -> reference:
"""
Handle a missing reference only if it is a ``lxml.etree._Element`` ref.
"""Handle a missing reference only if it is a ``lxml.etree._Element`` ref.

We handle only :class:`lxml.etree._Element` and :class:`~lxml.etree._Element` nodes.
"""
Expand All @@ -85,7 +84,7 @@ def lxml_element_doc_reference(app: Sphinx, env: BuildEnvironment, node: pending
and node.get('reftarget', None) == 'lxml.etree._Element'
and contnode.astext() in ('lxml.etree._Element', '_Element')
):
reftitle = '(in lxml v{})'.format(lxml.__version__) # type: ignore[attr-defined]
reftitle = f'(in lxml v{lxml.__version__})' # type: ignore[attr-defined]
newnode = reference('', '', internal=False, refuri=lxml_element_cls_doc_uri, reftitle=reftitle)
newnode.append(contnode)
return newnode
Expand Down
4 changes: 2 additions & 2 deletions doc/source/examples/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
key = xmlsec.Key.from_file('rsakey.pem', xmlsec.constants.KeyDataFormatPem)
manager.add_key(key)
enc_ctx = xmlsec.EncryptionContext(manager)
root = etree.parse("enc1-res.xml").getroot()
enc_data = xmlsec.tree.find_child(root, "EncryptedData", xmlsec.constants.EncNs)
root = etree.parse('enc1-res.xml').getroot()
enc_data = xmlsec.tree.find_child(root, 'EncryptedData', xmlsec.constants.EncNs)
decrypted = enc_ctx.decrypt(enc_data)
print(etree.tostring(decrypted))
24 changes: 7 additions & 17 deletions doc/source/examples/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
template,
xmlsec.constants.TransformAes128Cbc,
type=xmlsec.constants.TypeEncElement,
ns="xenc",
ns='xenc',
)

xmlsec.template.encrypted_data_ensure_cipher_value(enc_data)
key_info = xmlsec.template.encrypted_data_ensure_key_info(enc_data, ns="dsig")
key_info = xmlsec.template.encrypted_data_ensure_key_info(enc_data, ns='dsig')
enc_key = xmlsec.template.add_encrypted_key(key_info, xmlsec.constants.TransformRsaOaep)
xmlsec.template.encrypted_data_ensure_cipher_value(enc_key)
data = template.find('./Data')
Expand All @@ -24,20 +24,10 @@
manager.add_key(key)

enc_ctx = xmlsec.EncryptionContext(manager)
enc_ctx.key = xmlsec.Key.generate(
xmlsec.constants.KeyDataAes, 128, xmlsec.constants.KeyDataTypeSession
)
enc_ctx.key = xmlsec.Key.generate(xmlsec.constants.KeyDataAes, 128, xmlsec.constants.KeyDataTypeSession)
enc_data = enc_ctx.encrypt_xml(enc_data, data)
enc_method = xmlsec.tree.find_child(
enc_data, xmlsec.constants.NodeEncryptionMethod, xmlsec.constants.EncNs
)
key_info = xmlsec.tree.find_child(
enc_data, xmlsec.constants.NodeKeyInfo, xmlsec.constants.DSigNs
)
enc_method = xmlsec.tree.find_node(
key_info, xmlsec.constants.NodeEncryptionMethod, xmlsec.constants.EncNs
)
cipher_value = xmlsec.tree.find_node(
key_info, xmlsec.constants.NodeCipherValue, xmlsec.constants.EncNs
)
enc_method = xmlsec.tree.find_child(enc_data, xmlsec.constants.NodeEncryptionMethod, xmlsec.constants.EncNs)
key_info = xmlsec.tree.find_child(enc_data, xmlsec.constants.NodeKeyInfo, xmlsec.constants.DSigNs)
enc_method = xmlsec.tree.find_node(key_info, xmlsec.constants.NodeEncryptionMethod, xmlsec.constants.EncNs)
cipher_value = xmlsec.tree.find_node(key_info, xmlsec.constants.NodeCipherValue, xmlsec.constants.EncNs)
print(etree.tostring(cipher_value))
2 changes: 1 addition & 1 deletion doc/source/examples/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
with open('sign1-res.xml') as fp:
template = etree.parse(fp).getroot()

xmlsec.tree.add_ids(template, ["ID"])
xmlsec.tree.add_ids(template, ['ID'])
signature_node = xmlsec.tree.find_node(template, xmlsec.constants.NodeSignature)
# Create a digital signature context (no key manager is needed).
ctx = xmlsec.SignatureContext()
Expand Down
88 changes: 65 additions & 23 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4", "pkgconfig>=1.5.1", "lxml>=3.8, !=4.7.0"]

[tool.mypy]
files = ['src']
ignore_missing_imports = false
Expand All @@ -19,32 +22,71 @@ warn_no_return = true
no_implicit_reexport = true
show_error_codes = true

[tool.black]
line_length = 130
skip-string-normalization = true
target_version = ['py39']
include = '\.pyi?$'
exclude = '''
[tool.ruff]
# Maximum line length, same as your original Black + Flake8 config
line-length = 130

# Target Python version (used for autofixes and style rules)
target-version = "py39"

(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.mypy_cache
| \.tox
| build
| dist
)/
)
'''
# Directories and files to exclude from linting and formatting
exclude = [
".venv*", # virtual environments
".git", # git directory
"build", # build output
"dist", # distribution packages
"libs", # vendor libraries
".eggs", # setuptools egg folders
".direnv*", # direnv environments
"*_pb2.pyi" # protobuf-generated type stubs
]

[tool.isort]
profile = 'black'
known_first_party = ['xmlsec']
known_third_party = ['lxml', 'pytest', '_pytest', 'hypothesis']
[tool.ruff.lint]
# Enable rule categories:
# E = pycodestyle (style issues, like indentation, whitespace, etc.)
# F = pyflakes (unused imports, undefined names)
# I = isort (import sorting)
# B = flake8-bugbear (common bugs & anti-patterns)
# UP = pyupgrade (auto-upgrade syntax for newer Python)
# SIM = flake8-simplify (simplifiable code patterns)
# RUF = Ruff-native rules (extra, performance-optimized checks)
select = ["E", "F", "I", "B", "UP", "SIM", "RUF"]
# TODO: Add more rule categories as needed, e.g.:
# D = pydocstyle (docstring format/style issues)

[build-system]
requires = ['setuptools>=42', 'wheel', 'setuptools_scm[toml]>=3.4', "pkgconfig>=1.5.1", "lxml>=3.8, !=4.7.0"]
[tool.ruff.lint.per-file-ignores]
"*.pyi" = [
# Ignore formatting and import errors in stub files
"E301", # expected 1 blank line, found 0
"E302", # expected 2 blank lines, found 1
"E305", # expected 2 blank lines after class or function
"E501", # line too long
"E701", # multiple statements on one line
"F401", # unused import
"F811", # redefinition of unused name
"F822" # undefined name in `__all__`
]
"doc/source/conf.py" = [
"D1" # missing docstring in public module/class/function
]
"doc/source/examples/*.py" = [
"D1", # allow missing docstrings in examples
"E501" # allow long lines in code examples
]
"tests/*.py" = [
"D1" # allow missing docstrings in test files
]

[tool.ruff.format]
# Always use single quotes (e.g., 'text' instead of "text")
quote-style = "single"

# Format code with or without trailing commas
# true = prefer trailing commas where valid
skip-magic-trailing-comma = false

# Enforce Unix-style line endings (LF)
line-ending = "lf"

[tool.cibuildwheel]
build = [
Expand Down
6 changes: 4 additions & 2 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-r requirements.txt
pytest>=4.6.9
lxml-stubs

pytest==8.4.1
lxml-stubs==0.5.1
ruff[format]==0.12.3
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lxml >= 3.8.0, !=4.7.0
lxml==6.0.0
Loading
Loading