Skip to content

Commit 561384c

Browse files
tomschrppktscls19fr
committed
Fix #225: Deprecate module level functions
* Add test cases - Add additional test case for "check" - test_should_process_check_iscalled_with_valid_version - Test also missing finalize_version - Test the warning more thouroughly with pytest.warns instead of just pytest.deprecated_call * In `setup.cfg`, add deprecation warnings filter for pytest * Implement DeprecationWarning with warnings module and the new decorator `deprecated` * Output a DeprecationWarning for the following functions: - semver.bump_{major,minor,patch,prerelease,build} - semver.format_version - semver.finalize_version - semver.parse - semver.parse_version_info - semver.replace - semver.VersionInfo._asdict - semver.VersionInfo._astuple Add also a deprecation notice in the docstrings of these functions * Introduce new public functions: - semver.VersionInfo.to_dict (from former _asdict) - semver.VersionInfo.to_tuple (from former _astuple) - Keep _asdict and _astuple as a (deprecated) function for compatibility reasons * Update CHANGELOG.rst * Update usage documentation: - Add deprecation warning - Explain how to replace deprecated functions - Explain how to display deprecation warnings from semver * Improve documentation of deprecated functions - List deprecated module level functions - Make recommendation and show equivalent code - Mention that deprecated functions will be replaced in semver 3. That means, all deprecated function will be still available in semver 2.x.y. * Move _increment_string into VersionInfo class - Makes removing deprecating functions easier as, for example, bump_prerelease is no longer dependant from an "external" function. - Move _LAST_NUMBER regex into VersionInfo class - Implement _increment_string as a staticmethod Co-authored-by: Karol <karol@ppkt.eu> Co-authored-by: scls19fr <scls19fr@users.noreply.github.com>
1 parent 34f038d commit 561384c

File tree

6 files changed

+493
-145
lines changed

6 files changed

+493
-145
lines changed

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Files
1+
# Patch/Diff Files
22
*.patch
33
*.diff
4-
*.kate-swp
54

65
# Byte-compiled / optimized / DLL files
76
__pycache__/
87
*.py[cod]
98
.pytest_cache/
9+
*$py.class
1010

1111
# Distribution / packaging
1212
.cache
@@ -72,3 +72,7 @@ docs/_build/
7272

7373
# PyBuilder
7474
target/
75+
76+
# Backup files
77+
*~
78+
*.kate-swp

CHANGELOG.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,24 @@ Additions
2727

2828
* :pr:`228`: Added better doctest integration
2929

30+
3031
Removals
3132
--------
33+
* :gh:`225` (:pr:`229`): Output a DeprecationWarning for the following functions:
34+
35+
- ``semver.parse``
36+
- ``semver.parse_version_info``
37+
- ``semver.format_version``
38+
- ``semver.bump_{major,minor,patch,prerelease,build}``
39+
- ``semver.finalize_version``
40+
- ``semver.replace``
41+
- ``semver.VersionInfo._asdict`` (use the new, public available
42+
function ``semver.VersionInfo.to_dict()``)
43+
- ``semver.VersionInfo._astuple`` (use the new, public available
44+
function ``semver.VersionInfo.to_tuple()``)
45+
46+
These deprecated functions will be removed in semver 3.
47+
3248

3349

3450
Version 2.9.1

docs/usage.rst

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ are met.
1414
Knowing the Implemented semver.org Version
1515
------------------------------------------
1616

17-
The semver.org is the authorative specification of how semantical versioning is
18-
definied. To know which version of semver.org is implemented in the semver
19-
libary, use the following constant::
17+
The semver.org page is the authorative specification of how semantical
18+
versioning is definied.
19+
To know which version of semver.org is implemented in the semver libary,
20+
use the following constant::
2021

2122
>>> semver.SEMVER_SPEC_VERSION
2223
'2.0.0'
@@ -25,6 +26,26 @@ libary, use the following constant::
2526
Creating a Version
2627
------------------
2728

29+
Due to historical reasons, the semver project offers two ways of
30+
creating a version:
31+
32+
* through an object oriented approach with the :class:`semver.VersionInfo`
33+
class. This is the preferred method when using semver.
34+
35+
* through module level functions and builtin datatypes (usually strings
36+
and dicts).
37+
These method are still available for compatibility reasons, but are
38+
marked as deprecated. Using one of these will emit a DeprecationWarning.
39+
40+
41+
.. warning:: **Deprecation Warning**
42+
43+
Module level functions are marked as *deprecated* in version 2.9.2 now.
44+
These functions will be removed in semver 3.
45+
For details, see the sections :ref:`sec_replace_deprecated_functions` and
46+
:ref:`sec_display_deprecation_warnings`.
47+
48+
2849
A version can be created in different ways:
2950

3051
* as a complete version string::
@@ -362,8 +383,132 @@ For example:
362383

363384
.. code-block:: python
364385
365-
>>> coerce("v1.2")
386+
>>> coerce("v1.2")
366387
(VersionInfo(major=1, minor=2, patch=0, prerelease=None, build=None), '')
367388
>>> coerce("v2.5.2-bla")
368389
(VersionInfo(major=2, minor=5, patch=2, prerelease=None, build=None), '-bla')
369390
391+
392+
.. _sec_replace_deprecated_functions:
393+
394+
Replacing Deprecated Functions
395+
------------------------------
396+
397+
The development team of semver has decided to deprecate certain functions on
398+
the module level. The preferred way of using semver is through the
399+
:class:`semver.VersionInfo` class.
400+
401+
The deprecated functions can still be used in version 2.x.y. In version 3 of
402+
semver, the deprecated functions will be removed.
403+
404+
The following list shows the deprecated functions and how you can replace
405+
them with code which is compatible for future versions:
406+
407+
408+
* :func:`semver.bump_major`, :func:`semver.bump_minor`, :func:`semver.bump_patch`, :func:`semver.bump_prerelease`, :func:`semver.bump_build`
409+
410+
Replace them with the respective methods of the :class:`semver.VersionInfo`
411+
class.
412+
For example, the function :func:`semver.bump_major` is replaced by
413+
:func:`semver.VersionInfo.bump_major` and calling the ``str(versionobject)``:
414+
415+
.. code-block:: python
416+
417+
>>> s1 = semver.bump_major("3.4.5")
418+
>>> s2 = str(semver.VersionInfo.parse("3.4.5").bump_major())
419+
>>> s1 == s2
420+
True
421+
422+
Likewise with the other module level functions.
423+
424+
* :func:`semver.finalize_version`
425+
426+
Replace it with :func:`semver.VersionInfo.finalize_version`:
427+
428+
.. code-block:: python
429+
430+
>>> s1 = semver.finalize_version('1.2.3-rc.5')
431+
>>> s2 = str(semver.VersionInfo.parse('1.2.3-rc.5').finalize_version())
432+
>>> s1 == s2
433+
True
434+
435+
* :func:`semver.format_version`
436+
437+
Replace it with ``str(versionobject)``:
438+
439+
.. code-block:: python
440+
441+
>>> s1 = semver.format_version(5, 4, 3, 'pre.2', 'build.1')
442+
>>> s2 = str(semver.VersionInfo(5, 4, 3, 'pre.2', 'build.1'))
443+
>>> s1 == s2
444+
True
445+
446+
* :func:`semver.parse`
447+
448+
Replace it with :func:`semver.VersionInfo.parse` and
449+
:func:`semver.VersionInfo.to_dict`:
450+
451+
.. code-block:: python
452+
453+
>>> v1 = semver.parse("1.2.3")
454+
>>> v2 = semver.VersionInfo.parse("1.2.3").to_dict()
455+
>>> v1 == v2
456+
True
457+
458+
* :func:`semver.parse_version_info`
459+
460+
Replace it with :func:`semver.VersionInfo.parse`:
461+
462+
.. code-block:: python
463+
464+
>>> v1 = semver.parse_version_info("3.4.5")
465+
>>> v2 = semver.VersionInfo.parse("3.4.5")
466+
>>> v1 == v2
467+
True
468+
469+
* :func:`semver.replace`
470+
471+
Replace it with :func:`semver.VersionInfo.replace`:
472+
473+
.. code-block:: python
474+
475+
>>> s1 = semver.replace("1.2.3", major=2, patch=10)
476+
>>> s2 = str(semver.VersionInfo.parse('1.2.3').replace(major=2, patch=10))
477+
>>> s1 == s2
478+
True
479+
480+
481+
.. _sec_display_deprecation_warnings:
482+
483+
Displaying Deprecation Warnings
484+
-------------------------------
485+
486+
By default, deprecation warnings are `ignored in Python <https://docs.python.org/3/library/warnings.html#warning-categories>`_.
487+
This also affects semver's own warnings.
488+
489+
It is recommended that you turn on deprecation warnings in your scripts. Use one of
490+
the following methods:
491+
492+
* Use the option `-Wd <https://docs.python.org/3/using/cmdline.html#cmdoption-w>`_
493+
to enable default warnings:
494+
495+
* Directly running the Python command::
496+
497+
$ python3 -Wd scriptname.py
498+
499+
* Add the option in the shebang line (something like ``#!/usr/bin/python3``)
500+
after the command::
501+
502+
#!/usr/bin/python3 -Wd
503+
504+
* In your own scripts add a filter to ensure that *all* warnings are displayed:
505+
506+
.. code-block:: python
507+
508+
import warnings
509+
warnings.simplefilter("default")
510+
# Call your semver code
511+
512+
For further details, see the section
513+
`Overriding the default filter <https://docs.python.org/3/library/warnings.html#overriding-the-default-filter>`_
514+
of the Python documentation.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy