Skip to content

Commit

Permalink
feat: ValidationFailure is now ValidationError
Browse files Browse the repository at this point in the history
- ref https://peps.python.org/pep-0008/#exception-names
- import sorted using ruff
- adds project URLs
  • Loading branch information
yozachar committed Aug 1, 2023
1 parent 95ec1fa commit 12ae1f5
Show file tree
Hide file tree
Showing 42 changed files with 187 additions and 175 deletions.
8 changes: 5 additions & 3 deletions build_pkg.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Remove Refs."""

# standard
from subprocess import run

# from shutil import rmtree
from pathlib import Path
from subprocess import run # nosec

# local
from docs.gen_docs import generate_documentation
Expand All @@ -13,5 +12,8 @@
project_dir = Path(__file__).parent
generate_documentation(project_dir, only_rst_man=True)
print()
process = run(("poetry", "build"), capture_output=True)
process = run(("poetry", "build"), capture_output=True) # nosec
print(process.stderr.decode() + process.stdout.decode())


# TODO: Address all '# nosec'
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"""

# standard
from importlib.metadata import metadata
from datetime import datetime
from importlib.metadata import metadata

# -- Project information ----------------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
Expand Down
18 changes: 10 additions & 8 deletions docs/gen_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# -*- coding: utf-8 -*-

# standard
from shutil import rmtree, move, copy
from ast import parse, ImportFrom
from typing import List, Dict
from ast import ImportFrom, parse
from os.path import getsize
from subprocess import run
from pathlib import Path
from shutil import copy, move, rmtree
from subprocess import run # nosec
from typing import Dict, List

__all__ = ("generate_documentation",)

Expand Down Expand Up @@ -53,7 +53,7 @@ def _generate_reference(source: Path, destination: Path, ext: str):
def _update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, List[str]]):
"""Temporary update to mkdocs config."""
# external
from yaml import safe_load, safe_dump
from yaml import safe_dump, safe_load

copy(source, destination)
with open(source, "rt") as mkf:
Expand All @@ -69,7 +69,7 @@ def _gen_md_docs(source: Path, refs_path: Path):
# backup mkdocs config
_update_mkdocs_config(source / "mkdocs.yaml", source / "mkdocs.bak.yaml", nav_items)
# build mkdocs as subprocess
mkdocs_build = run(("mkdocs", "build"), capture_output=True)
mkdocs_build = run(("mkdocs", "build"), capture_output=True) # nosec
print(mkdocs_build.stderr.decode() + mkdocs_build.stdout.decode())
# restore mkdocs config
move(str(source / "mkdocs.bak.yaml"), source / "mkdocs.yaml")
Expand All @@ -96,12 +96,12 @@ def _gen_rst_docs(source: Path, refs_path: Path, only_web: bool = False, only_ma
rc = 0
if not only_man:
# build sphinx web pages as subprocess
web_build = run(("sphinx-build", "docs", "docs/_build/web"), capture_output=True)
web_build = run(("sphinx-build", "docs", "docs/_build/web"), capture_output=True) # nosec
print(web_build.stderr.decode() + web_build.stdout.decode())
rc = web_build.returncode
if not only_web:
# build sphinx man pages as subprocess
man_build = run(
man_build = run( # nosec
("sphinx-build", "-b", "man", "docs", "docs/_build/man"), capture_output=True
)
print(man_build.stderr.decode() + man_build.stdout.decode())
Expand Down Expand Up @@ -155,3 +155,5 @@ def generate_documentation(
# # for debugging
)
quit(rc)

# TODO: Address all '# nosec'
40 changes: 28 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ name = "validators"
version = "0.21.1"
description = "Python Data Validation for Humans™"
authors = [{ name = "Konsta Vesterinen", email = "konsta@fastmonkeys.com" }]
requires-python = ">=3.8"
readme = "README.md"
license = { text = "MIT" }
readme = "README.md"
keywords = ["validation", "validator", "python-validator"]
classifiers = [
"Development Status :: 4 - Beta",
Expand All @@ -26,12 +25,18 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Libraries :: Python Modules",
]
requires-python = ">=3.8"
dependencies = []

####################
# Dependencies #
####################
[project.urls]
Homepage = "https://python-validators.github.io/validators"
Documentation = "https://python-validators.github.io/validators"
Repository = "https://github.com/python-validators/validators"
Changelog = "https://github.com/python-validators/validators/blob/master/CHANGES.md"

dependencies = []
###########################
# Optional Dependencies #
###########################

[project.optional-dependencies]
docs-offline = ["myst-parser>=2.0.0", "pypandoc-binary>=1.11", "sphinx>=7.1.1"]
Expand Down Expand Up @@ -75,12 +80,15 @@ line-length = 100
target-version = ["py38", "py39", "py310", "py311"]

[tool.pyright]
include = ["src", "tests"]
extraPaths = ["src"]
exclude = ["**/__pycache__", ".pytest_cache/", ".tox/", ".venv/", "site/"]
pythonVersion = "3.8"
pythonPlatform = "All"
typeCheckingMode = "strict"

[tool.pytest.ini_options]
pythonpath = ["src"]

[tool.ruff]
select = ["E", "F", "I", "N", "D"]
line-length = 100
Expand All @@ -107,25 +115,33 @@ env_list = lint, type, format, sast, py{38,39,310,311}
[testenv:lint]
description = ruff linter
deps = ruff
deps =
ruff
commands = ruff check .
[testenv:type]
description = pyright type checker
deps = pyright
deps =
pyright
pyaml
pypandoc-binary
pytest
commands = pyright .
[testenv:format]
description = code formatter
deps = black
deps =
black
commands = black .
[testenv:sast]
deps = bandit[toml]
deps =
bandit[toml]
commands = bandit -c pyproject.toml -r .
[testenv]
description = unit tests
deps = pytest
deps =
pytest
commands = pytest .
"""
4 changes: 2 additions & 2 deletions src/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .mac_address import mac_address
from .slug import slug
from .url import url
from .utils import validator, ValidationFailure
from .utils import validator, ValidationError
from .uuid import uuid

# from .crypto_addresses import eth_address
Expand Down Expand Up @@ -57,7 +57,7 @@
"unionpay",
"url",
"uuid",
"ValidationFailure",
"ValidationError",
"validator",
"visa",
# i18n
Expand Down
4 changes: 2 additions & 2 deletions src/validators/between.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def between(
>>> between(13.2, min_val=13, max_val=14)
# Output: True
>>> between(500, max_val=400)
# Output: ValidationFailure(func=between, args=...)
# Output: ValidationError(func=between, args=...)
>>> between(
... datetime(2000, 11, 11),
... min_val=datetime(1999, 11, 11)
Expand All @@ -54,7 +54,7 @@ def between(
Returns:
(Literal[True]):
If `value` is in between the given conditions.
(ValidationFailure):
(ValidationError):
If `value` is not in between the given conditions.
Raises:
Expand Down
4 changes: 2 additions & 2 deletions src/validators/btc_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def btc_address(value: str, /):
>>> btc_address('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69')
# Output: True
>>> btc_address('1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2')
# Output: ValidationFailure(func=btc_address, args=...)
# Output: ValidationError(func=btc_address, args=...)
Args:
value:
Expand All @@ -45,7 +45,7 @@ def btc_address(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid bitcoin address.
(ValidationFailure):
(ValidationError):
If `value` is an invalid bitcoin address.
> *New in version 0.18.0*.
Expand Down
32 changes: 16 additions & 16 deletions src/validators/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def card_number(value: str, /):
>>> card_number('4242424242424242')
# Output: True
>>> card_number('4242424242424241')
# Output: ValidationFailure(func=card_number, args={'value': '4242424242424241'})
# Output: ValidationError(func=card_number, args={'value': '4242424242424241'})
Args:
value:
Expand All @@ -29,7 +29,7 @@ def card_number(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid generic card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid generic card number.
> *New in version 0.15.0*.
Expand All @@ -53,7 +53,7 @@ def visa(value: str, /):
>>> visa('4242424242424242')
# Output: True
>>> visa('2223003122003222')
# Output: ValidationFailure(func=visa, args={'value': '2223003122003222'})
# Output: ValidationError(func=visa, args={'value': '2223003122003222'})
Args:
value:
Expand All @@ -62,7 +62,7 @@ def visa(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid Visa card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid Visa card number.
> *New in version 0.15.0*.
Expand All @@ -79,7 +79,7 @@ def mastercard(value: str, /):
>>> mastercard('5555555555554444')
# Output: True
>>> mastercard('4242424242424242')
# Output: ValidationFailure(func=mastercard, args={'value': '4242424242424242'})
# Output: ValidationError(func=mastercard, args={'value': '4242424242424242'})
Args:
value:
Expand All @@ -88,7 +88,7 @@ def mastercard(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid Mastercard card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid Mastercard card number.
> *New in version 0.15.0*.
Expand All @@ -105,7 +105,7 @@ def amex(value: str, /):
>>> amex('378282246310005')
# Output: True
>>> amex('4242424242424242')
# Output: ValidationFailure(func=amex, args={'value': '4242424242424242'})
# Output: ValidationError(func=amex, args={'value': '4242424242424242'})
Args:
value:
Expand All @@ -114,7 +114,7 @@ def amex(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid American Express card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid American Express card number.
> *New in version 0.15.0*.
Expand All @@ -131,7 +131,7 @@ def unionpay(value: str, /):
>>> unionpay('6200000000000005')
# Output: True
>>> unionpay('4242424242424242')
# Output: ValidationFailure(func=unionpay, args={'value': '4242424242424242'})
# Output: ValidationError(func=unionpay, args={'value': '4242424242424242'})
Args:
value:
Expand All @@ -140,7 +140,7 @@ def unionpay(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid UnionPay card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid UnionPay card number.
> *New in version 0.15.0*.
Expand All @@ -157,7 +157,7 @@ def diners(value: str, /):
>>> diners('3056930009020004')
# Output: True
>>> diners('4242424242424242')
# Output: ValidationFailure(func=diners, args={'value': '4242424242424242'})
# Output: ValidationError(func=diners, args={'value': '4242424242424242'})
Args:
value:
Expand All @@ -166,7 +166,7 @@ def diners(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid Diners Club card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid Diners Club card number.
> *New in version 0.15.0*.
Expand All @@ -183,7 +183,7 @@ def jcb(value: str, /):
>>> jcb('3566002020360505')
# Output: True
>>> jcb('4242424242424242')
# Output: ValidationFailure(func=jcb, args={'value': '4242424242424242'})
# Output: ValidationError(func=jcb, args={'value': '4242424242424242'})
Args:
value:
Expand All @@ -192,7 +192,7 @@ def jcb(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid JCB card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid JCB card number.
> *New in version 0.15.0*.
Expand All @@ -209,7 +209,7 @@ def discover(value: str, /):
>>> discover('6011111111111117')
# Output: True
>>> discover('4242424242424242')
# Output: ValidationFailure(func=discover, args={'value': '4242424242424242'})
# Output: ValidationError(func=discover, args={'value': '4242424242424242'})
Args:
value:
Expand All @@ -218,7 +218,7 @@ def discover(value: str, /):
Returns:
(Literal[True]):
If `value` is a valid Discover card number.
(ValidationFailure):
(ValidationError):
If `value` is an invalid Discover card number.
> *New in version 0.15.0*.
Expand Down
4 changes: 2 additions & 2 deletions src/validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def domain(value: str, /, *, rfc_1034: bool = False, rfc_2782: bool = False):
>>> domain('example.com')
# Output: True
>>> domain('example.com/')
# Output: ValidationFailure(func=domain, ...)
# Output: ValidationError(func=domain, ...)
>>> # Supports IDN domains as well::
>>> domain('xn----gtbspbbmkef.xn--p1ai')
# Output: True
Expand All @@ -35,7 +35,7 @@ def domain(value: str, /, *, rfc_1034: bool = False, rfc_2782: bool = False):
Returns:
(Literal[True]):
If `value` is a valid domain name.
(ValidationFailure):
(ValidationError):
If `value` is an invalid domain name.
Note:
Expand Down
Loading

0 comments on commit 12ae1f5

Please sign in to comment.