From 5020c9c5b9d40e00dcc1244e11e8740db80260f3 Mon Sep 17 00:00:00 2001 From: Will Gordon Date: Fri, 19 May 2023 19:48:11 -0400 Subject: [PATCH] Removes deprecated CUID class - Includes a CLI command for generating CUID strings - Adds a continuous integration GitHub Action --- .github/workflows/continuous_integration.yml | 26 ++++++++++ README.md | 22 ++++++++- local/tests/test_collision.py | 4 +- local/tests/test_cuid.py | 39 ++++++++++----- pdm.lock | 50 ++++++++------------ pyproject.toml | 15 +++--- src/cuid2/__init__.py | 5 +- src/cuid2/cli.py | 8 ++++ src/cuid2/generator.py | 25 +++++----- 9 files changed, 127 insertions(+), 67 deletions(-) create mode 100644 .github/workflows/continuous_integration.yml create mode 100644 src/cuid2/cli.py diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml new file mode 100644 index 0000000..891d8f9 --- /dev/null +++ b/.github/workflows/continuous_integration.yml @@ -0,0 +1,26 @@ +name: Continuous Integration +on: + workflow_dispatch: {} + schedule: + # Weekly every Saturday at midnight + - cron: '0 0 * * 6' + +jobs: + test-lint-scan: + uses: ./.github/workflows/test_lint_scan.yml + + collision-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up PDM + uses: pdm-project/setup-pdm@v3 + with: + python-version: 3.11 + - name: Install dependencies + run: | + pdm install -dG test + - name: Run Testing + run: | + pdm run -v testing-slow \ No newline at end of file diff --git a/README.md b/README.md index 691fb2e..72ee125 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,29 @@ pip install cuid2 ``` ## Usage -You can generate CUIDs with the following: + +You can generate CUIDs directly in the terminal with the following: +```bash +$ cuid2 +``` + +Or you can rely on a CUID wrapper if you don't need any customizations: +```python +from typing import Callable +from cuid2 import cuid_wrapper + +cuid_generator: Callable[[], str] = cuid_wrapper() + +def main(): + my_cuid: str = cuid_generator() + next_cuid: str = cuid_generator() +``` + +Finally, for more explicit control of the CUID generator, you can instantiate the class directly: ```python from cuid2 import Cuid -CUID_GENERATOR: Cuid = Cuid() +CUID_GENERATOR: Cuid = Cuid(length=10) def main(): my_cuid: str = CUID_GENERATOR.generate() diff --git a/local/tests/test_collision.py b/local/tests/test_collision.py index b270d04..efbc49a 100644 --- a/local/tests/test_collision.py +++ b/local/tests/test_collision.py @@ -6,7 +6,7 @@ import pytest -from cuid2 import CUID +from cuid2 import Cuid def cuid_generator(max_ids: int) -> Dict[str, Any]: @@ -31,7 +31,7 @@ def cuid_generator(max_ids: int) -> Dict[str, Any]: def _id_to_int(value: str, radix: int = str_base) -> int: return reduce(lambda r, v: r * radix + int(v, radix), value, 0) - cuid: Callable[[], str] = CUID().generate + cuid: Callable[[], str] = Cuid().generate bucket_count: int = 20 result: dict[str, Any] = { diff --git a/local/tests/test_cuid.py b/local/tests/test_cuid.py index d642384..83de65a 100644 --- a/local/tests/test_cuid.py +++ b/local/tests/test_cuid.py @@ -1,11 +1,11 @@ from math import floor from secrets import SystemRandom from timeit import repeat -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Callable import pytest -from cuid2 import CUID, DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid +from cuid2 import DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid, cuid_wrapper if TYPE_CHECKING: from unittest.mock import Mock @@ -163,15 +163,28 @@ def test_generate_uses_base36_encoding(self: "TestCuid", mocker: "Mock") -> None assert len(result) == DEFAULT_LENGTH assert result == "l9j3ikop1bi8tcvzme3x3yv7" - # Tests that instantiating the CUID class provides a deprecation warning. - def test_constructor_deprecation_warning(self: "TestCuid") -> None: - with pytest.deprecated_call(): - cuid = CUID() - assert isinstance(cuid, Cuid) - # Tests that subclassing the CUID class provides a deprecation warning. - def test_subclass_deprecation_warning(self: "TestCuid") -> None: - with pytest.deprecated_call(): - - class CuidSubclass(CUID): - pass +class TestCuidWrapper: + def test_cuid_wrapper_is_callable(self: "TestCuidWrapper") -> None: + """Tests that the function returns a callable.""" + cuid_func: Callable[[], str] = cuid_wrapper() + assert callable(cuid_func) + + def test_cuid_wrapper_return_type(self: "TestCuidWrapper") -> None: + """Tests that the function returns a callable that generates a CUID string.""" + cuid_func: Callable[[], str] = cuid_wrapper() + assert isinstance(cuid_func(), str) + + def test_cuid_wrapper_no_parameters(self: "TestCuidWrapper") -> None: + """Tests that the function doesn't accept any parameters.""" + cuid_func: Callable[[], str] = cuid_wrapper() + with pytest.raises( + TypeError, + match="cuid\\(\\) takes 0 positional arguments but 1 was given", + ): + cuid_func(DEFAULT_LENGTH) # type: ignore[call-arg] + + def test_cuid_wrapper_length(self: "TestCuidWrapper") -> None: + """Tests that the function returns a callable that generates a CUID string with the default length.""" + cuid_func: Callable[[], str] = cuid_wrapper() + assert len(cuid_func()) == DEFAULT_LENGTH diff --git a/pdm.lock b/pdm.lock index 51a2c57..50fc362 100644 --- a/pdm.lock +++ b/pdm.lock @@ -229,12 +229,6 @@ dependencies = [ "tomli>=2.0.1; python_version < \"3.11\"", ] -[[package]] -name = "pyscan-rs" -version = "0.1.1" -requires_python = ">=3.7" -summary = "Python dependency vulnerability scanner" - [[package]] name = "pytest" version = "7.3.1" @@ -297,7 +291,7 @@ summary = "C version of reader, parser and emitter for ruamel.yaml derived from [[package]] name = "ruff" -version = "0.0.267" +version = "0.0.269" requires_python = ">=3.7" summary = "An extremely fast Python linter, written in Rust." @@ -408,7 +402,7 @@ summary = "Module for decorators, wrappers and monkey patching." lock_version = "4.2" cross_platform = true groups = ["default", "lint", "security", "test", "tox", "typing"] -content_hash = "sha256:788b9ef0ee25a235f9697fda44a480de9bd41cd3f571627f63d28939468a00cf" +content_hash = "sha256:6a1317a469e319ab4b333af06ac07a02a357724b02d5de94ed9220ed1e0f6df7" [metadata.files] "astroid 2.15.4" = [ @@ -733,10 +727,6 @@ content_hash = "sha256:788b9ef0ee25a235f9697fda44a480de9bd41cd3f571627f63d289394 {url = "https://files.pythonhosted.org/packages/23/6f/d02d1cab4a30998806687036e03ffecbf66e35df6bfab5a29f22ec61a075/pyproject_api-1.5.1.tar.gz", hash = "sha256:435f46547a9ff22cf4208ee274fca3e2869aeb062a4834adfc99a4dd64af3cf9"}, {url = "https://files.pythonhosted.org/packages/3e/39/5b1d0ac2c8fddd06b68edb38cf813e0659d940f7781ab38e8ade1e3387a1/pyproject_api-1.5.1-py3-none-any.whl", hash = "sha256:4698a3777c2e0f6b624f8a4599131e2a25376d90fe8d146d7ac74c67c6f97c43"}, ] -"pyscan-rs 0.1.1" = [ - {url = "https://files.pythonhosted.org/packages/8c/0c/44043830fae70da837398d5050cdbd0b0a548f5f7fff289ced1da781445a/pyscan_rs-0.1.1.tar.gz", hash = "sha256:90839709f934e361d69555a43c06f59c9e1a06fe5d4ef101669eba210ffbbe42"}, - {url = "https://files.pythonhosted.org/packages/8c/bd/5fccc287cf7735a8d06a183c3e56253005b87cc23064a5e7b41f30cd50ef/pyscan_rs-0.1.1-py3-none-win_amd64.whl", hash = "sha256:4f01f283bde922f283aa99f95d72f07b8f2dac185c70a7e072930bb00589d0d8"}, -] "pytest 7.3.1" = [ {url = "https://files.pythonhosted.org/packages/1b/d1/72df649a705af1e3a09ffe14b0c7d3be1fd730da6b98beb4a2ed26b8a023/pytest-7.3.1-py3-none-any.whl", hash = "sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362"}, {url = "https://files.pythonhosted.org/packages/ec/d9/36b65598f3d19d0a14d13dc87ad5fa42869ae53bb7471f619a30eaabc4bf/pytest-7.3.1.tar.gz", hash = "sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3"}, @@ -795,24 +785,24 @@ content_hash = "sha256:788b9ef0ee25a235f9697fda44a480de9bd41cd3f571627f63d289394 {url = "https://files.pythonhosted.org/packages/fc/47/a8e865b6097969e162c2e9efcfed8ded0582ea18305b09426dd648a6b2d4/ruamel.yaml.clib-0.2.7-cp311-cp311-macosx_12_6_arm64.whl", hash = "sha256:721bc4ba4525f53f6a611ec0967bdcee61b31df5a56801281027a3a6d1c2daf5"}, {url = "https://files.pythonhosted.org/packages/ff/66/4c05485243e24c6db5d7305063304c410b5539577becc89e4539d2897e41/ruamel.yaml.clib-0.2.7-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:bf9a6bc4a0221538b1a7de3ed7bca4c93c02346853f44e1cd764be0023cd3640"}, ] -"ruff 0.0.267" = [ - {url = "https://files.pythonhosted.org/packages/1e/6c/a5a28b7c4df1316ff0ede0ce51af3a9af76b23c6f19a2d9edcd28036aa8d/ruff-0.0.267-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:5a898953949e37c109dd242cfcf9841e065319995ebb7cdfd213b446094a942f"}, - {url = "https://files.pythonhosted.org/packages/21/80/c68a5d5117f84b6d4892cf8a5e1b164bd5b14bc0894ce0cba5343e40f132/ruff-0.0.267-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:9adf1307fa9d840d1acaa477eb04f9702032a483214c409fca9dc46f5f157fe3"}, - {url = "https://files.pythonhosted.org/packages/3e/52/d1015fa5a13e9bd11b7a4e1147f541e52fe666b550f14e9ca1249bdc447b/ruff-0.0.267-py3-none-musllinux_1_2_i686.whl", hash = "sha256:786de30723c71fc46b80a173c3313fc0dbe73c96bd9da8dd1212cbc2f84cdfb2"}, - {url = "https://files.pythonhosted.org/packages/45/1e/f423a80da2217137ef3d287aec70d978ae71038cce92cdc61659ebb4a063/ruff-0.0.267-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:2107cec3699ca4d7bd41543dc1d475c97ae3a21ea9212238b5c2088fa8ee7722"}, - {url = "https://files.pythonhosted.org/packages/53/0c/8897fc309498b48fc60552c714264e91fda10c44defa9401a5c6288cb198/ruff-0.0.267-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bbe104f21a429b77eb5ac276bd5352fd8c0e1fbb580b4c772f77ee8c76825654"}, - {url = "https://files.pythonhosted.org/packages/53/49/fe3e44b5ebd9456e4e0acf79d9c0a777da59f170b7b95e2438f7e10deb15/ruff-0.0.267-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:45d61a2b01bdf61581a2ee039503a08aa603dc74a6bbe6fb5d1ce3052f5370e5"}, - {url = "https://files.pythonhosted.org/packages/54/e3/eaa63e3a5c03613088ca00857ba4cf6e6e0e7463a183633a2083b167d02f/ruff-0.0.267-py3-none-win_amd64.whl", hash = "sha256:d09aecc9f5845586ba90911d815f9772c5a6dcf2e34be58c6017ecb124534ac4"}, - {url = "https://files.pythonhosted.org/packages/56/8f/0ea8e8023f2005e86d9177a250ac0bda008143e5d38f3c21301d81fc09ac/ruff-0.0.267-py3-none-win32.whl", hash = "sha256:d12ab329474c46b96d962e2bdb92e3ad2144981fe41b89c7770f370646c0101f"}, - {url = "https://files.pythonhosted.org/packages/5d/ba/5dfcde964c63c37e825889dc706afd8641c5460a01eeb6764b2440d1ba25/ruff-0.0.267-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:f731d81cb939e757b0335b0090f18ca2e9ff8bcc8e6a1cf909245958949b6e11"}, - {url = "https://files.pythonhosted.org/packages/67/85/249e6d52ed6272cdb9785e00bd8f1f7b7ac9ed3285c42300137faabb50ae/ruff-0.0.267-py3-none-win_arm64.whl", hash = "sha256:7df7eb5f8d791566ba97cc0b144981b9c080a5b861abaf4bb35a26c8a77b83e9"}, - {url = "https://files.pythonhosted.org/packages/6d/7d/1c3a89baa73a7880db0706092b668061c172cda6a7ac510646af54bd746a/ruff-0.0.267-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:67254ae34c38cba109fdc52e4a70887de1f850fb3971e5eeef343db67305d1c1"}, - {url = "https://files.pythonhosted.org/packages/96/e4/c9a745407e204e755fa39a1819dc602662fef90fd9b00f44aac3b4d67176/ruff-0.0.267-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:0afca3633c8e2b6c0a48ad0061180b641b3b404d68d7e6736aab301c8024c424"}, - {url = "https://files.pythonhosted.org/packages/b8/3e/ef28832b87a177b3815c6e1fbe314498359122d671e8d80eca22a933784f/ruff-0.0.267-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:20c594eb56c19063ef5a57f89340e64c6550e169d6a29408a45130a8c3068adc"}, - {url = "https://files.pythonhosted.org/packages/d9/da/ee9b62a02de95d716522095e62e7b2fb94dba13d9943e27400c59813bfb5/ruff-0.0.267.tar.gz", hash = "sha256:632cec7bbaf3c06fcf0a72a1dd029b7d8b7f424ba95a574aaa135f5d20a00af7"}, - {url = "https://files.pythonhosted.org/packages/f1/7a/08f4803d440ee8fac3f007ca41e1a061fa529833c90b3b8dd37662393bbf/ruff-0.0.267-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:db33deef2a5e1cf528ca51cc59dd764122a48a19a6c776283b223d147041153f"}, - {url = "https://files.pythonhosted.org/packages/fa/25/cd00b70d749cff37dc567e7267926211ddfaeb8b4eb05aed43d0decfceea/ruff-0.0.267-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2972241065b1c911bce3db808837ed10f4f6f8a8e15520a4242d291083605ab6"}, - {url = "https://files.pythonhosted.org/packages/fb/09/6db9f4e8f5dbbe82a72abf2b89cc2bd5033ebee9767187428061e93b4e17/ruff-0.0.267-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:4adbbbe314d8fcc539a245065bad89446a3cef2e0c9cf70bf7bb9ed6fe31856d"}, +"ruff 0.0.269" = [ + {url = "https://files.pythonhosted.org/packages/02/65/c49103428b27ef3831f6f8023a062de07d7a88d4d9d9f22cbf4941331b4b/ruff-0.0.269-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:f062059b8289a4fab7f6064601b811d447c2f9d3d432a17f689efe4d68988450"}, + {url = "https://files.pythonhosted.org/packages/18/f1/28f24e47a8dfb72762056713fd66c2f6fd543d0f6d3858291cb3d4993c2a/ruff-0.0.269-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1f19f59ca3c28742955241fb452f3346241ddbd34e72ac5cb3d84fadebcf6bc8"}, + {url = "https://files.pythonhosted.org/packages/1d/97/60d69db70fa1e7e29a553fc6719ef6b750da0d570285f4c890ba686655f8/ruff-0.0.269-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6da8ee25ef2f0cc6cc8e6e20942c1d44d25a36dce35070d7184655bc14f63f63"}, + {url = "https://files.pythonhosted.org/packages/26/64/3e1fc0ccf7d59d7c45ec643541642cd3bcc95c36b5c2e01f9f9f908650c4/ruff-0.0.269-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:cec2f4b84a14b87f1b121488649eb5b4eaa06467a2387373f750da74bdcb5679"}, + {url = "https://files.pythonhosted.org/packages/2a/22/514380df775cac977caaa1394e2955c600e6f3895c0713e5f5aa99735d02/ruff-0.0.269-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3f5dc7aac52c58e82510217e3c7efd80765c134c097c2815d59e40face0d1fe6"}, + {url = "https://files.pythonhosted.org/packages/30/ff/e1e0ec0e4e31b3f163ef3e80456150a41a3b910cac04472a13248b812ce6/ruff-0.0.269-py3-none-win_arm64.whl", hash = "sha256:bbeb857b1e508a4487bdb02ca1e6d41dd8d5ac5335a5246e25de8a3dff38c1ff"}, + {url = "https://files.pythonhosted.org/packages/3c/42/88f5242e41988bb83a37a04f16f3523c4b65b2133fa5e9d05ef276dd0b70/ruff-0.0.269-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:5a20658f0b97d207c7841c13d528f36d666bf445b00b01139f28a8ccb80093bb"}, + {url = "https://files.pythonhosted.org/packages/4a/04/77ad67155861ccf0deead144e4ba766b54c669c37aafc25d092a1a4f0af7/ruff-0.0.269.tar.gz", hash = "sha256:11ddcfbab32cf5c420ea9dd5531170ace5a3e59c16d9251c7bd2581f7b16f602"}, + {url = "https://files.pythonhosted.org/packages/5b/93/67a13d30ae7d709774ab14b12ba6e90d3d8116a19afae1f0a5a818513ea1/ruff-0.0.269-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a374434e588e06550df0f8dcb74777290f285678de991fda4e1063c367ab2eb2"}, + {url = "https://files.pythonhosted.org/packages/95/19/0c3372acb612100e7417e394b85d98e3a73d80c6cc5a86d0253a40bc68ca/ruff-0.0.269-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:374b161753a247904aec7a32d45e165302b76b6e83d22d099bf3ff7c232c888f"}, + {url = "https://files.pythonhosted.org/packages/9c/38/1ce6bc423e9e86ed21e4cae62a0f67f08ddc8bfdadcd2c340e427fd3e9a1/ruff-0.0.269-py3-none-win_amd64.whl", hash = "sha256:f3b59ccff57b21ef0967ea8021fd187ec14c528ec65507d8bcbe035912050776"}, + {url = "https://files.pythonhosted.org/packages/a9/a4/b555c6fb6880c64cd88e42e44c235770abf6263457ab493dac4ad123c427/ruff-0.0.269-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:bd81b8e681b9eaa6cf15484f3985bd8bd97c3d114e95bff3e8ea283bf8865062"}, + {url = "https://files.pythonhosted.org/packages/b9/9f/692b03ed8f2065e4fd628d5f55ba68fe941cd23bd6f9afa4eddcba27316b/ruff-0.0.269-py3-none-win32.whl", hash = "sha256:03ff42bc91ceca58e0f0f072cb3f9286a9208f609812753474e799a997cdad1a"}, + {url = "https://files.pythonhosted.org/packages/bf/0b/6415002df30de68a541f0fce1adb77d8160b5afb36edeea2e0c3fbf28192/ruff-0.0.269-py3-none-musllinux_1_2_i686.whl", hash = "sha256:9ca0a1ddb1d835b5f742db9711c6cf59f213a1ad0088cb1e924a005fd399e7d8"}, + {url = "https://files.pythonhosted.org/packages/d4/7c/1b8378f80962ff7d129a39785964710f92ebd9fa91e2444f00635ba3f510/ruff-0.0.269-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:3569bcdee679045c09c0161fabc057599759c49219a08d9a4aad2cc3982ccba3"}, + {url = "https://files.pythonhosted.org/packages/f6/0c/eced078f3e0a4461b10dbf163160bd7a860378303a07253cbe8d1541fff4/ruff-0.0.269-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:56347da63757a56cbce7d4b3d6044ca4f1941cd1bbff3714f7554360c3361f83"}, + {url = "https://files.pythonhosted.org/packages/f8/17/90e1cb087a20e136da10b0187cf1bf25ac80da1c5fcb0256b683e6eee047/ruff-0.0.269-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e131b4dbe798c391090c6407641d6ab12c0fa1bb952379dde45e5000e208dabb"}, ] "safety 2.4.0b1" = [ {url = "https://files.pythonhosted.org/packages/8f/9c/a69152151c26247b050609bca55eea2d6d95c055883df0bf6a64444a4057/safety-2.4.0b1.tar.gz", hash = "sha256:26b3000eec09f64fdd323db29c44c0446607b0c9b4ce65c3f8f9570e2c640958"}, diff --git a/pyproject.toml b/pyproject.toml index 5ba2c55..3f51849 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,9 @@ keywords = ['crypt', 'security', 'uuid', 'guid', 'cuid', 'cryptography'] [project.urls] repository = "https://github.com/gordon-code/cuid2/" +[project.scripts] +cuid2 = "cuid2.cli:main" + [tool.pdm.version] source = "scm" @@ -49,22 +52,22 @@ lint = [ "black~=23.3.0", # https://github.com/psf/black (latest: 23.3.0) "codespell~=2.2.4", # https://github.com/codespell-project/codespell (latest: 2.2.4) "pylint~=2.17.4", # https://github.com/PyCQA/pylint (latest: 2.17.4) - "ruff~=0.0.267", # https://github.com/charliermarsh/ruff (latest: 0.0.267) - "safety==2.4.0b1", # https://github.com/pyupio/safety (latest: 2.3.5) + "ruff~=0.0.269", # https://github.com/charliermarsh/ruff (latest: 0.0.269) + "safety==2.4.0b1", # https://github.com/pyupio/safety (latest: 2.3.5) ] test = [ "pytest~=7.3.1", # https://github.com/pytest-dev/pytest (latest: 7.3.1) "pytest-mock~=3.10.0", # https://github.com/pytest-dev/pytest-mock/ (latest: 3.10.0) "pytest-sugar~=0.9.7", # https://github.com/Teemu/pytest-sugar/ (latest: 0.9.7) ] -typing = [ - "mypy~=1.3.0", # https://github.com/python/mypy (latest: 1.3.0) -] tox = [ # Version reduced to prevent `packaging` conflict with safety - "tox~=4.4.12", # https://github.com/tox-dev/tox (latest: 4.5.1) + "tox~=4.4.12", # https://github.com/tox-dev/tox (latest: 4.5.1) "tox-pdm~=0.6.1", # https://github.com/pdm-project/tox-pdm (latest: 0.6.1) ] +typing = [ + "mypy~=1.3.0", # https://github.com/python/mypy (latest: 1.3.0) +] [tool.tox] legacy_tox_ini = """ diff --git a/src/cuid2/__init__.py b/src/cuid2/__init__.py index 06cc541..baf0f49 100644 --- a/src/cuid2/__init__.py +++ b/src/cuid2/__init__.py @@ -1,5 +1,4 @@ """Next generation GUIDs. Collision-resistant ids optimized for horizontal scaling and performance.""" +from .generator import DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid, cuid_wrapper -from .generator import CUID, DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid - -__all__ = ["Cuid", "CUID", "DEFAULT_LENGTH", "INITIAL_COUNT_MAX"] +__all__ = ["Cuid", "DEFAULT_LENGTH", "INITIAL_COUNT_MAX", "cuid_wrapper"] diff --git a/src/cuid2/cli.py b/src/cuid2/cli.py new file mode 100644 index 0000000..c38330d --- /dev/null +++ b/src/cuid2/cli.py @@ -0,0 +1,8 @@ +from cuid2.generator import cuid_wrapper + +generate_cuid = cuid_wrapper() + + +def main() -> None: + """Print out a CUID generated string. Used by the CLI console script.""" + print(generate_cuid()) # noqa: T201 (print statement) diff --git a/src/cuid2/generator.py b/src/cuid2/generator.py index c105671..757c75c 100644 --- a/src/cuid2/generator.py +++ b/src/cuid2/generator.py @@ -1,8 +1,7 @@ import time -import warnings from math import floor from secrets import SystemRandom -from typing import TYPE_CHECKING, Any, Callable, Dict, Final, Optional, Protocol, Tuple, Type +from typing import TYPE_CHECKING, Callable, Final, Optional, Protocol from cuid2 import utils @@ -99,13 +98,17 @@ def generate(self: "Cuid", length: Optional[int] = None) -> str: return first_letter + utils.create_hash(hash_input)[1 : length or self._length] -class CUID(Cuid): # pylint: disable=too-few-public-methods - def __init_subclass__(cls: Type["CUID"], **kwargs: Dict[str, Any]) -> None: - """Deprecated class CUID. Use Cuid instead.""" - warnings.warn("CUID is deprecated, use Cuid instead", DeprecationWarning, stacklevel=1) - super().__init_subclass__(**kwargs) +def cuid_wrapper() -> Callable[[], str]: + """Wrap a single Cuid class instance and return a callable that generates a CUID string. - def __new__(cls: Type["CUID"], *args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> "CUID": - """Deprecated class CUID. Use Cuid instead.""" - warnings.warn("CUID is deprecated, use Cuid instead", DeprecationWarning, stacklevel=1) - return super().__new__(cls, *args, **kwargs) + Returns + ------- + Callable[[], str] + A callable that generates a CUID string. + """ + cuid_generator: Cuid = Cuid() + + def cuid() -> str: + return cuid_generator.generate() + + return cuid