12
12
13
13
"""
14
14
15
+ # flake8: noqa: E402
16
+
15
17
from __future__ import annotations
16
18
17
19
import re
@@ -419,8 +421,8 @@ def decorator1(func: _FT) -> _FT:
419
421
elif isinstance (exception , BaseException ):
420
422
def decorator2 (func : _FT ) -> _FT :
421
423
@wraps (func )
422
- def wrapper (* args , ** kwargs ) :
423
- raise exception
424
+ def wrapper (* args : Any , ** kwargs : Any ) -> Any :
425
+ raise exception # type: ignore[misc]
424
426
return wrapper # type: ignore[return-value]
425
427
return decorator2
426
428
@@ -503,7 +505,41 @@ def wrapper(*args: Any, **kwargs: Any) -> None:
503
505
raise TypeError (f"{ exception .__class__ .__name__ !r} " )
504
506
505
507
506
- _PATTERN = re .compile ("([0-9]+).([0-9]+).([0-9]+)" )
508
+ # See PEP 440 Apendix B
509
+ _PATTERN_STR = r"""
510
+ v?
511
+ (?:
512
+ (?:(?P<epoch>[0-9]+)!)? # epoch
513
+ (?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
514
+ (?P<pre> # pre-release
515
+ [-_\.]?
516
+ (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
517
+ [-_\.]?
518
+ (?P<pre_n>[0-9]+)?
519
+ )?
520
+ (?P<post> # post release
521
+ (?:-(?P<post_n1>[0-9]+))
522
+ |
523
+ (?:
524
+ [-_\.]?
525
+ (?P<post_l>post|rev|r)
526
+ [-_\.]?
527
+ (?P<post_n2>[0-9]+)?
528
+ )
529
+ )?
530
+ (?P<dev> # dev release
531
+ [-_\.]?
532
+ (?P<dev_l>dev)
533
+ [-_\.]?
534
+ (?P<dev_n>[0-9]+)?
535
+ )?
536
+ )
537
+ (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
538
+ """
539
+ _PATTERN = re .compile (
540
+ r"^\s*" + _PATTERN_STR + r"\s*$" ,
541
+ re .VERBOSE | re .IGNORECASE ,
542
+ )
507
543
508
544
509
545
class VersionInfo (NamedTuple ):
@@ -525,16 +561,26 @@ class VersionInfo(NamedTuple):
525
561
major : int
526
562
527
563
#: :class:`int`: The semantic_ minor version.
528
- minor : int
564
+ minor : int = 0
529
565
530
- #: :class:`int`: The semantic_ micro version (a.k.a. :attr:`VersionInfo.patch`) .
531
- micro : int
566
+ #: :class:`int`: The semantic_ micro version.
567
+ micro : int = 0
532
568
533
569
@property
534
570
def patch (self ) -> int :
535
571
""":class:`int`: An alias for :attr:`VersionInfo.micro`."""
536
572
return self .micro
537
573
574
+ @property
575
+ def maintenance (self ) -> int :
576
+ """:class:`int`: An alias for :attr:`VersionInfo.micro`."""
577
+ return self .micro
578
+
579
+ @property
580
+ def bug (self ) -> int :
581
+ """:class:`int`: An alias for :attr:`VersionInfo.micro`."""
582
+ return self .micro
583
+
538
584
@classmethod
539
585
def from_str (
540
586
cls ,
@@ -547,23 +593,27 @@ def from_str(
547
593
Parameters
548
594
----------
549
595
version : :class:`str`
550
- A string representation of a version (*e.g.* :code:`version = "0.8.2"`).
551
- The string should contain three ``"."`` separated integers, respectively,
552
- representing the major, minor and micro/patch versions.
596
+ A PEP 440-compatible version string(*e.g.* :code:`version = "0.8.2"`).
597
+ Note that version representations are truncated at up to three integers.
553
598
fullmatch : :class:`bool`
554
- Whether the version-string must consist exclusivelly of three
555
- period-separated integers, or if a substring is also allowed.
599
+ Whether to perform a full or partial match on the passed string.
556
600
557
601
Returns
558
602
-------
559
603
:class:`nanoutils.VersionInfo`
560
604
A new VersionInfo instance.
561
605
606
+ See Also
607
+ --------
608
+ :pep:`440`
609
+ This PEP describes a scheme for identifying versions of Python software distributions,
610
+ and declaring dependencies on particular versions.
611
+
562
612
"""
563
613
match = _PATTERN .fullmatch (version ) if fullmatch else _PATTERN .match (version )
564
614
if match is None :
565
615
raise ValueError (f"Failed to parse { version !r} " )
566
- return cls ._make (int (i ) for i in match . groups () )
616
+ return cls ._make (int (i ) for i in match [ "release" ]. split ( "." )[: 3 ] )
567
617
568
618
569
619
def _get_directive (
@@ -797,10 +847,10 @@ def warning_filter(
797
847
:func:`warnings.filterwarnings` :
798
848
Insert a simple entry into the list of warnings filters (at the front).
799
849
800
- """
850
+ """ # noqa: E501
801
851
def decorator (func : _FT ) -> _FT :
802
852
@functools .wraps (func )
803
- def wrapper (* args , ** kwargs ) :
853
+ def wrapper (* args : Any , ** kwargs : Any ) -> Any :
804
854
with warnings .catch_warnings ():
805
855
warnings .filterwarnings (action , message , category , module , lineno , append )
806
856
ret = func (* args , ** kwargs )
0 commit comments