Skip to content

maint: misc fixes and improvements #249

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 1 commit into from
Mar 20, 2023
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
26 changes: 12 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ repos:
- id: debug-statements
- id: destroyed-symlinks
- id: trailing-whitespace
# will be uncommented when all the
# scripts have been revised once
# - repo: https://github.com/psf/black
# rev: 23.1.0
# hooks:
# - id: black
# - repo: https://github.com/PyCQA/isort
# rev: 5.12.0
# hooks:
# - id: isort
# - repo: https://github.com/PyCQA/flake8
# rev: 6.0.0
# hooks:
# - id: flake8
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
10 changes: 6 additions & 4 deletions docs/gen_docs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Generate docs."""

# standard
from shutil import copy, move, rmtree
from yaml import safe_load, safe_dump
from shutil import rmtree, move, copy
from ast import parse, ImportFrom
from typing import Dict, List
from typing import List, Dict
from os.path import getsize
from subprocess import run
from pathlib import Path
from sys import argv

# external
from yaml import safe_load, safe_dump


def _write_ref_content(source: Path, module_name: str, func_name: str):
"""Write content."""
Expand Down Expand Up @@ -61,7 +63,7 @@ def generate_documentation(source: Path):
# build docs as subprocess
print(run(("mkdocs", "build"), capture_output=True).stderr.decode())
# restore mkdocs config
move(source / "mkdocs.bak.yml", source / "mkdocs.yml")
move(str(source / "mkdocs.bak.yml"), source / "mkdocs.yml")


if __name__ == "__main__":
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 29 additions & 28 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tox = "^4.4.7"
mkdocs = "^1.4.2"
mkdocs-material = "^9.1.3"
mkdocstrings = { extras = ["python"], version = "^0.20.0" }
pyaml = "^21.10.1"

[tool.poetry.group.sast.dependencies]
bandit = "^1.7.5"
Expand All @@ -60,38 +61,31 @@ build-backend = "poetry.core.masonry.api"
[tool.black]
line-length = 100
target-version = ["py38", "py39", "py310", "py311"]
extend-exclude = "i18n"

[tool.bandit]
exclude_dirs = [
".github",
".pytest_cache",
".tox",
".vscode",
"docs",
"site",
"tests",
]
exclude_dirs = [".github", ".pytest_cache", ".tox", ".vscode", "site", "tests"]

[tool.isort]
extend_skip_glob = ["**/i18n/*"]
ensure_newline_before_comments = true
extend_skip_glob = ["**/i18n/*"]
force_grid_wrap = 0
force_sort_within_sections = true
ignore_comments = true
include_trailing_comma = true
known_local_folder = ["validators"]
line_length = 100
length_sort = true
line_length = 100
multi_line_output = 3
profile = "black"
reverse_sort = true
reverse_relative = true
reverse_sort = true
skip_gitignore = true
use_parentheses = true

[tool.pyright]
include = ["validators", "tests"]
exclude = ["**/__pycache__", ".pytest_cache/", ".tox/", "site/"]
exclude = ["**/__pycache__", ".pytest_cache/", ".tox/", "site/", "**/i18n/*"]
pythonVersion = "3.8"
pythonPlatform = "All"
typeCheckingMode = "strict"
Expand All @@ -102,25 +96,32 @@ legacy_tox_ini = """
requires =
tox >= 4.0
env_list = py{38,39,310,311}
# format, lint, type,
fmt_black, fmt_isort, lint, type

[testenv]
description = run unit tests
deps = pytest
commands = pytest

# [testenv:format]
# description = run formatter
# deps = black
# commands = black

# [testenv:lint]
# description = run linters
# deps = flake8
# commands = flake8

# [testenv:type]
# description = run type checker
# deps = pyright
# commands = pyright
[testenv:fmt_black]
description = run formatter
deps = black
commands = black .

[testenv:fmt_isort]
description = run formatter
deps = isort
commands = isort .

[testenv:lint]
description = run linters
deps = flake8
commands = flake8

[testenv:type]
description = run type checker
deps =
pyright
pytest
commands = pyright
"""
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# until https://github.com/PyCQA/flake8/issues/234 is resolved

[flake8]
max-line-length = 100
docstring-convention = google
exclude = __pycache__,.github,.pytest_cache,.tox,.vscode,site,i18n
max-line-length = 100
4 changes: 2 additions & 2 deletions tests/test__extremes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
# external
import pytest

# project
from validators._extremes import AbsMax, AbsMin
# local
from validators._extremes import AbsMin, AbsMax

abs_max = AbsMax()
abs_min = AbsMin()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

# local
from validators import (
ValidationFailure,
card_number,
mastercard,
unionpay,
Expand All @@ -15,6 +14,7 @@
visa,
amex,
jcb,
ValidationFailure,
)

visa_cards = ["4242424242424242", "4000002760003184"]
Expand Down
3 changes: 1 addition & 2 deletions tests/test_hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import pytest

# local
from validators import md5, sha1, sha224, sha256, sha512, ValidationFailure

from validators import sha512, sha256, sha224, sha1, md5, ValidationFailure

# ==> md5 <== #

Expand Down
2 changes: 1 addition & 1 deletion tests/test_ip_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

# local
from validators import ipv4, ipv6, ValidationFailure
from validators import ipv6, ipv4, ValidationFailure


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# external
import pytest

# project
# local
from validators import length, ValidationFailure


Expand Down
2 changes: 1 addition & 1 deletion tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

# local
from validators import ValidationFailure, url
from validators import url, ValidationFailure


@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions tests/test_validation_failure.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Test validation Failure."""
# -*- coding: utf-8 -*-

# project
# local
from validators import between


failed_obj_repr = "ValidationFailure(func=between"


Expand Down
22 changes: 11 additions & 11 deletions validators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"""Validate Anything!"""
# -*- coding: utf-8 -*-

from .between import between
from .card import card_number, mastercard, unionpay, discover, diners, visa, amex, jcb
from .hashes import sha512, sha256, sha224, sha1, md5
from .utils import validator, ValidationFailure
from .i18n import fi_business_id, fi_ssn
from .mac_address import mac_address
from .btc_address import btc_address
from .card import amex, card_number, diners, discover, jcb, mastercard, unionpay, visa
from .domain import domain
from .email import email
from .hashes import md5, sha1, sha224, sha256, sha512
from .ip_address import ipv6, ipv4
from .hostname import hostname
from .i18n import fi_business_id, fi_ssn
from .iban import iban
from .ip_address import ipv4, ipv6
from .between import between
from .length import length
from .mac_address import mac_address
from .domain import domain
from .email import email
from .uuid import uuid
from .slug import slug
from .iban import iban
from .url import url
from .utils import ValidationFailure, validator
from .uuid import uuid

__all__ = (
"amex",
Expand Down
2 changes: 1 addition & 1 deletion validators/between.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime

# local
from ._extremes import AbsMax, AbsMin
from ._extremes import AbsMin, AbsMax
from .utils import validator

PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime)
Expand Down
17 changes: 9 additions & 8 deletions validators/btc_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ def btc_address(value: str, /):

> *New in version 0.18.0*.
"""
if value and type(value) is str:
return (
# segwit pattern
re.compile(r"^(bc|tc)[0-3][02-9ac-hj-np-z]{14,74}$").match(value)
if value[:2] in ("bc", "tb")
else _validate_old_btc_address(value)
)
return False
if not value:
return False

return (
# segwit pattern
re.compile(r"^(bc|tc)[0-3][02-9ac-hj-np-z]{14,74}$").match(value)
if value[:2] in ("bc", "tb")
else _validate_old_btc_address(value)
)
2 changes: 2 additions & 0 deletions validators/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def card_number(value: str, /):

> *New in version 0.15.0*.
"""
if not value:
return False
try:
digits = list(map(int, value))
odd_sum = sum(digits[-1::-2])
Expand Down
2 changes: 2 additions & 0 deletions validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def domain(value: str, /, *, rfc_1034: bool = False, rfc_2782: bool = False):

> *New in version 0.9.0*.
"""
if not value:
return False
try:
return not re.search(r"\s", value) and re.match(
# First character of the domain
Expand Down
10 changes: 5 additions & 5 deletions validators/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def md5(value: str, /):

> *New in version 0.12.1*
"""
return re.match(r"^[0-9a-f]{32}$", value, re.IGNORECASE)
return re.match(r"^[0-9a-f]{32}$", value, re.IGNORECASE) if value else False


@validator
Expand All @@ -55,7 +55,7 @@ def sha1(value: str, /):

> *New in version 0.12.1*
"""
return re.match(r"^[0-9a-f]{40}$", value, re.IGNORECASE)
return re.match(r"^[0-9a-f]{40}$", value, re.IGNORECASE) if value else False


@validator
Expand All @@ -80,7 +80,7 @@ def sha224(value: str, /):

> *New in version 0.12.1*
"""
return re.match(r"^[0-9a-f]{56}$", value, re.IGNORECASE)
return re.match(r"^[0-9a-f]{56}$", value, re.IGNORECASE) if value else False


@validator
Expand Down Expand Up @@ -108,7 +108,7 @@ def sha256(value: str, /):

> *New in version 0.12.1*
"""
return re.match(r"^[0-9a-f]{64}$", value, re.IGNORECASE)
return re.match(r"^[0-9a-f]{64}$", value, re.IGNORECASE) if value else False


@validator
Expand Down Expand Up @@ -137,4 +137,4 @@ def sha512(value: str, /):

> *New in version 0.12.1*
"""
return re.match(r"^[0-9a-f]{128}$", value, re.IGNORECASE)
return re.match(r"^[0-9a-f]{128}$", value, re.IGNORECASE) if value else False
Loading