j7i|fddlmZddlmZddlmZddlmZddlmZddlmZddl Z ddl m Z ddl Z ddl Z dd lmZdd lmZerdd lmZ dd ZGd dZdZGddeZGddeZGddeZGddeZGddeZdd dZd!dZd"dZd#dZy)$) annotations) Collection)Mapping)Sequence)Sized)DecimalN)Complex)Any) TYPE_CHECKING)ndarrayct|}|jddgd}|D]W\}} } t|dt||d<t|dt| |d<t|dt| |d<Ydt|d|dd |d |g|D cgc](\} } } | d |dd d | d |dd d | d |dd *c} } } z} | Scc} } } w)Nr)IndexObtainedExpected)rrrz(comparison failed. Mismatched elements: z / :zMax absolute difference: zMax relative difference: <z | )listinsertmaxlen) full_object message_datanumber_of_elements different_ids max_abs_diff max_rel_diff message_list max_sizesindexobtainedexpectedindexes explanations [/mnt/ssd/data/python-lab/ChefSystem/venv/lib/python3.12/site-packages/_pytest/python_api.py_compare_approxr(s2 %L<=I%18!x9Q<U4 ! 9Q<X7 ! 9Q<X7 ! 8 33}3E2FcJ\I]]^_ #L>2 #L>2 ,8   'GXx 1Yq\N " #3x)A,p/?&@HQyYZ|n\\L\C]^  K   s-C cbeZdZdZdZdZd ddZddZddZddZ dZ dZ dd Z dd Z d Zdd Zy) ApproxBasezhProvide shared utilities for making approximate comparisons between numbers or sequences of numbers.Ndc`d}||_||_||_||_|j y)NT)r$absrelnan_ok _check_type)selfr$r.r-r/__tracebackhide__s r'__init__zApproxBase.__init__:s0     ctNNotImplementedErrorr1s r'__repr__zApproxBase.__repr__Bs!!r4cdd|d|gS)Nzcomparison failedz Obtained: z Expected: )r1 other_sides r' _repr_comparezApproxBase._repr_compareEs#  %   r4cJtfdj|DS)Nc3LK|]\}}|j|k(ywr6_approx_scalar).0axr1s r' z$ApproxBase.__eq__..Ms* ,0AqA$$Q' ' s!$)all_yield_comparisonsr1actuals` r'__eq__zApproxBase.__eq__Ls( 484K4KF4S   r4cd}td)NTzVapprox() is not supported in a boolean context. Did you mean: `assert a == approx(b)`?)AssertionError)r1r2s r'__bool__zApproxBase.__bool__Qs  e  r4c||k( Sr6r<rIs r'__ne__zApproxBase.__ne__ZsdN##r4ct|tr-t||j|j|j St ||j|j|j S)N)r.r-r/) isinstancer ApproxDecimalr.r-r/ ApproxScalar)r1rEs r'rBzApproxBase._approx_scalar]sF a ! dhht{{S SA488$++NNr4ct)zoYield all the pairs of numbers to be compared. This is used to implement the `__eq__` method. r7rIs r'rHzApproxBase._yield_comparisonsbs "!r4cy)zrKrN__hash__rPrBrHr0r<r4r'r*r*2sJ(O"   H$O "Kr4r*ct|ttzrt|}|fd|DSt |r|Dcgc]}t |c}S|Scc}w)z=Recursively map a function over a sequence of arbitrary depthc36K|]}t|ywr6)_recursive_sequence_map)rCxifs r'rFz*_recursive_sequence_map..vsC2/26Cs)rRrtupletype_is_sequence_likeri)rkrEseq_typerjs` r'ririrsZ!TE\"7CCCC 1 9:;2'2.;;t .get_value_from_nested_lists&%E !a !Lr4z3Impossible to compare arrays with different shapes.zShapes:  and c32K|]}t|ywr6)range)rCr}s r'rFz,ApproxNumpy._repr_compare..s(Jaq(Js)rzz list[Any]r{ztuple[Any, ...]rYr ) itertoolsmathr$shaperirBrv_as_numpy_arraysizeinfproductr-rappendr\r()r1r=rrr~np_array_shapeapprox_side_as_seqother_side_as_arrayrrrrr" approx_value other_valueabs_diffrs r'r>zApproxNumpy._repr_compares " .=   ,,4   !5!5!7  .j9"... 066 6E>*%0C0I0I/JK  "]]//y y  &Y&&(J>(JK ,E56H%PL45H%PK{*|44{BC"<: #%#'88L#&|XK@P5P#QL$$U+ ,$'   E ./BEJK./A5IJ    MM         s5F"cddl}|j|s |j|}|j|s$|j |j j k7ryt|!|S#t$r}t d|d|d}~wwxYw)Nrzcannot compare 'z' to numpy.ndarrayF) numpyisscalarasarray Exception TypeErrorrr$superrK)r1rJnpe __class__s r'rKzApproxNumpy.__eq__s{{6" VF+{{6"v||t}}7J7J'Jw~f%%  V"26(:L MNTUU VsA-- B 6BB c#Kddl}|j|rL|j|jjD]#}||j|j f%y|j|jjD]4}||j |j|j f6yw)Nr)rrndindexr$ritem)r1rJrr}s r'rHzApproxNumpy._yield_comparisonss ;;v ZZ 3 34 6dmmA.33555 6ZZ 3 34 @Qinn& a(8(=(=(??? @sB?Cr[)r=zndarray | list[Any]rYr]r^) r`rarbrcr:r>rKrH __classcell__rs@r'rqrq}sR+ > @& @r4rqcBeZdZdZddZddZd fd ZdZd dZxZ S) ApproxMappingzyPerform approximate comparisons where the expected value is a mapping with numeric values (the keys can be anything).c d|jjDcic]\}}||j|c}}dScc}}wrs)r$itemsrB)r1kvs r'r:zApproxMapping.__repr__sB$--BUBUBWX$!QAt22155X\\]^^XsAc ddl}t|jt|k7r&ddt|jdt|gSt|jj t|j k7r0dd|jj d|j gS|jj Dcic]\}}||j |}}}t|}|j }|j }g} t|j |jd D]\\} } } | | k7s| jp| n t|t| j| z }| jd k(r |j}n/t|t| j| z | jz }| j| | D cgc](} t| t|| t|| f*}} t|j||| ||Scc}}w#t$rYqwxYwcc} w) Nrz4Impossible to compare mappings with different sizes. Lengths: rzcomparison failed.z&Mappings has different keys: expected z but got Tstrictr)rrr$setkeysrrBrzipvaluesrr-ZeroDivisionErrorrr\r()r1r=rrrapprox_side_as_maprrrr approx_keyrrkeyrs r'r>zApproxMapping._repr_compares\ t}} Z 0FC ./uS_4EF  t}}!!# $JOO,=(> >$89K9K9M8NiXbXgXgXiWjk  37--2E2E2G *.!QAt""1% %  !!34y y  7:  $ $ & (9(9(;D8  1 3 &Z {*((49P'*(#l.C.Ck.Q*R( (00C7+/88L+. , #%1%:%:[%H&2&;&;%<!",L$$Z0+ 12% Xs:c?+S1CC1H-I J   MM        I 6- sH1$A-H7(-I7 IIc t|jt|jjk7ry t||S#t$rYywxYwNF)rrr$AttributeErrorrrKr1rJrs r'rKzApproxMapping.__eq__(sZ 6;;=!S););)=%>>? w~f%%  s=A AAc#vK|jjD]}|||j|fywr6)r$r)r1rJrs r'rHz ApproxMapping._yield_comparisons1s:##% .A)T]]1-- - .s79c d}|jjD]_\}}t|t|js&d}t |j ||t j|jy)NTz[pytest.approx() does not support nested dictionaries: key={!r} value={!r} full mapping={})r$rrRrmrformatpprintpformat)r1r2rr|msgs r'r0zApproxMapping._check_type5sg ----/ WJC%dmm!45t 3v~~dmm7T UVV Wr4r[)r=zMapping[object, float]rYr]r^r_ r`rarbrcr:r>rKrHr0rrs@r'rrs$7_: x&.Wr4rcBeZdZdZddZddZd fd ZdZd dZxZ S) ApproxSequenceLikezRPerform approximate comparisons where the expected value is a sequence of numbers.ctj}|ttfvrt}d|fdjDdS)Nrtc3@K|]}j|ywr6rA)rCrEr1s r'rFz.ApproxSequenceLike.__repr__..Ds!PQ$"5"5a"8!Pru)rmr$rlr)r1ros` r'r:zApproxSequenceLike.__repr__@sB & E4= (H!P$--!PPSSTUUr4c ddl}t|jt|k7r&ddt|jdt|gSt|j|j}t|}|j }|j }g}t t||dD]o\}\} } | | k7s t| j| z } t|| }| dk(r |j }nt|| t| z } |j|q|Dcgc](}t|t||t||f*} }t|j| ||||S#t$rYkwxYwcc}w)Nrz1Impossible to compare lists with different sizes.rrTrr)rrr$rirBr enumeraterr-rrrr\r() r1r=rrrrrrr}rrrrs r'r>z ApproxSequenceLike._repr_compareFs t}} Z 0CC ./uS_4EF  5T5H5H$--X !34y y  .7 "Jt </  ( *A* k{* V"<#8#8;#FGH#&|X#>L #c)'+xx '*<C DT9T'U $$Q' ($# VSA'-?-B)C D   MM        ! s:$E) -E8) E54E5c t|t|jk7ry t||S#t$rYywxYwr)rr$rrrKrs r'rKzApproxSequenceLike.__eq__ssJ 6{c$--001w~f%%  s!5 AAc2t||jdS)NTr)rr$rIs r'rHz%ApproxSequenceLike._yield_comparisons{s64==66r4c d}t|jD]_\}}t|t|js&d}t |j ||t j|jy)NTz]pytest.approx() does not support nested data structures: {!r} at index {} full sequence: {})rr$rRrmrrrr)r1r2r"rErs r'r0zApproxSequenceLike._check_type~sc !$--0 UHE1!T$--01v 1eV^^DMM5R STT Ur4r[)r=zSequence[float]rYr]r^r_rrs@r'rr=s"\V + Z&7Ur4rcReZdZUdZdZded<dZded<d dZd dZd Z e d Z y ) rTzLPerform approximate comparisons where the expected value is a single number.g-q=zfloat | DecimalDEFAULT_ABSOLUTE_TOLERANCEgư>DEFAULT_RELATIVE_TOLERANCEct|jtset|jttzrDt j t|jxst|jtrt|jS d|jcxkrdkrnn|jd}n|jd}t|jtr:|jjr$t j |js|dz }|jd|S#t$rd}YwxYw)uReturn a string communicating both the expected value and the tolerance for the comparison being made. For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``. gMbP?g@@n.1eu ∠ ±180°??? ± ) rRr$rXr rrisinfr-r\ toleranceimag ValueError)r1vetted_tolerances r'r:zApproxScalar.__repr__s t}}d +t}}g.?@zz#dmm,O 4==$0OPt}}% % %t~~++&*nnQ%7 &*nnS%9 4=='2MM&& 4>>2 N2 --%5$677 %$  %sB D00 D>=D>cdd}t|}|tfd|jDS|jr ||sy|jk(ry|js8t jt t zrt |t t zsytjtjr,jxrtjt|Stjtjrytj|z jk}|S)ziReturn whether the given value is equal to the expected value within the pre-specified tolerance.ct|trytjj dx}rt||j Sy)NTrF)rRrXsysmodulesgetbool_)valrs r'is_boolz$ApproxScalar.__eq__..is_bools;#t$[[__W--r-!#rxx00r4c3@K|]}j|ywr6)rK)rCrDr1s r'rFz&ApproxScalar.__eq__..s.set_defaults 1 27 2r4rz&absolute tolerance can't be negative: z absolute tolerance can't be NaN.z&relative tolerance can't be negative: z relative tolerance can't be NaN.) r-rrrrr.rr$r)r1rabsolute_tolerancerelative_tolerances r'rzApproxScalar.tolerances 3 )43R3RS  !89K8LM  ::( )?@ @ 88 xx#))) HHd55    !89K8LM  ::( )?@ @%'9::r4r[r^) r`rarbrcr__annotations__rr:rKrfpropertyrr<r4r'rTrTsBV387266!8F3jH ,;,;r4rTc8eZdZdZedZedZddZy)rSzFPerform approximate comparisons where the expected value is a Decimal.z1e-12z1e-6ct|jtr tj|j}n |j}t|j tr tj|j }n |j }d}|%td|cxkrtdkrnn|d}n||d}|j d|S)Nrz1e-31e3rr)rRr.floatr from_floatr-r$)r1r.abs_tol_strs r'r:zApproxDecimal.__repr__s dhh &$$TXX.C((C dhh &%%dhh/D88D ?wv#GGS G  c G--WI..r4Nr[)r`rarbrcrrrr:r<r4r'rSrSsP!(!1!(/r4rScPd}t|trt}nt|trt}nlt |rt |}t}nOt|rt}n=t|tr't|ttzsd|}t|t}|||||S)a!Assert that two numbers (or two ordered sequences of numbers) are equal to each other within some tolerance. Due to the :doc:`python:tutorial/floatingpoint`, numbers that we would intuitively expect to be equal are not always so:: >>> 0.1 + 0.2 == 0.3 False This problem is commonly encountered when writing tests, e.g. when making sure that floating-point values are what you expect them to be. One way to deal with this problem is to assert that two floating-point numbers are equal to within some appropriate tolerance:: >>> abs((0.1 + 0.2) - 0.3) < 1e-6 True However, comparisons like this are tedious to write and difficult to understand. Furthermore, absolute comparisons like the one above are usually discouraged because there's no tolerance that works well for all situations. ``1e-6`` is good for numbers around ``1``, but too small for very big numbers and too big for very small ones. It's better to express the tolerance as a fraction of the expected value, but relative comparisons like that are even more difficult to write correctly and concisely. The ``approx`` class performs floating-point comparisons using a syntax that's as intuitive as possible:: >>> from pytest import approx >>> 0.1 + 0.2 == approx(0.3) True The same syntax also works for ordered sequences of numbers:: >>> (0.1 + 0.2, 0.2 + 0.4) == approx((0.3, 0.6)) True ``numpy`` arrays:: >>> import numpy as np # doctest: +SKIP >>> np.array([0.1, 0.2]) + np.array([0.2, 0.4]) == approx(np.array([0.3, 0.6])) # doctest: +SKIP True And for a ``numpy`` array against a scalar:: >>> import numpy as np # doctest: +SKIP >>> np.array([0.1, 0.2]) + np.array([0.2, 0.1]) == approx(0.3) # doctest: +SKIP True Only ordered sequences are supported, because ``approx`` needs to infer the relative position of the sequences without ambiguity. This means ``sets`` and other unordered sequences are not supported. Finally, dictionary *values* can also be compared:: >>> {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6}) True The comparison will be true if both mappings have the same keys and their respective values match the expected tolerances. **Tolerances** By default, ``approx`` considers numbers within a relative tolerance of ``1e-6`` (i.e. one part in a million) of its expected value to be equal. This treatment would lead to surprising results if the expected value was ``0.0``, because nothing but ``0.0`` itself is relatively close to ``0.0``. To handle this case less surprisingly, ``approx`` also considers numbers within an absolute tolerance of ``1e-12`` of its expected value to be equal. Infinity and NaN are special cases. Infinity is only considered equal to itself, regardless of the relative tolerance. NaN is not considered equal to anything by default, but you can make it be equal to itself by setting the ``nan_ok`` argument to True. (This is meant to facilitate comparing arrays that use NaN to mean "no data".) Both the relative and absolute tolerances can be changed by passing arguments to the ``approx`` constructor:: >>> 1.0001 == approx(1) False >>> 1.0001 == approx(1, rel=1e-3) True >>> 1.0001 == approx(1, abs=1e-3) True If you specify ``abs`` but not ``rel``, the comparison will not consider the relative tolerance at all. In other words, two numbers that are within the default relative tolerance of ``1e-6`` will still be considered unequal if they exceed the specified absolute tolerance. If you specify both ``abs`` and ``rel``, the numbers will be considered equal if either tolerance is met:: >>> 1 + 1e-8 == approx(1) True >>> 1 + 1e-8 == approx(1, abs=1e-12) False >>> 1 + 1e-8 == approx(1, rel=1e-6, abs=1e-12) True **Non-numeric types** You can also use ``approx`` to compare non-numeric types, or dicts and sequences containing non-numeric types, in which case it falls back to strict equality. This can be useful for comparing dicts and sequences that can contain optional values:: >>> {"required": 1.0000005, "optional": None} == approx({"required": 1, "optional": None}) True >>> [None, 1.0000005] == approx([None,1]) True >>> ["foo", 1.0000005] == approx([None,1]) False If you're thinking about using ``approx``, then you might want to know how it compares to other good ways of comparing floating-point numbers. All of these algorithms are based on relative and absolute tolerances and should agree for the most part, but they do have meaningful differences: - ``math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)``: True if the relative tolerance is met w.r.t. either ``a`` or ``b`` or if the absolute tolerance is met. Because the relative tolerance is calculated w.r.t. both ``a`` and ``b``, this test is symmetric (i.e. neither ``a`` nor ``b`` is a "reference value"). You have to specify an absolute tolerance if you want to compare to ``0.0`` because there is no tolerance by default. More information: :py:func:`math.isclose`. - ``numpy.isclose(a, b, rtol=1e-5, atol=1e-8)``: True if the difference between ``a`` and ``b`` is less that the sum of the relative tolerance w.r.t. ``b`` and the absolute tolerance. Because the relative tolerance is only calculated w.r.t. ``b``, this test is asymmetric and you can think of ``b`` as the reference value. Support for comparing sequences is provided by :py:func:`numpy.allclose`. More information: :std:doc:`numpy:reference/generated/numpy.isclose`. - ``unittest.TestCase.assertAlmostEqual(a, b)``: True if ``a`` and ``b`` are within an absolute tolerance of ``1e-7``. No relative tolerance is considered , so this function is not appropriate for very large or very small numbers. Also, it's only available in subclasses of ``unittest.TestCase`` and it's ugly because it doesn't follow PEP8. More information: :py:meth:`unittest.TestCase.assertAlmostEqual`. - ``a == pytest.approx(b, rel=1e-6, abs=1e-12)``: True if the relative tolerance is met w.r.t. ``b`` or if the absolute tolerance is met. Because the relative tolerance is only calculated w.r.t. ``b``, this test is asymmetric and you can think of ``b`` as the reference value. In the special case that you explicitly specify an absolute tolerance but not a relative tolerance, only the absolute tolerance is considered. .. note:: ``approx`` can handle numpy arrays, but we recommend the specialised test helpers in :std:doc:`numpy:reference/routines.testing` if you need support for comparisons, NaNs, or ULP-based tolerances. To match strings using regex, you can use `Matches `_ from the `re_assert package `_. .. note:: Unlike built-in equality, this function considers booleans unequal to numeric zero or one. For example:: >>> 1 == approx(True) False .. warning:: .. versionchanged:: 3.2 In order to avoid inconsistent behavior, :py:exc:`TypeError` is raised for ``>``, ``>=``, ``<`` and ``<=`` comparisons. The example below illustrates the problem:: assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10) assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10) In the second example one expects ``approx(0.1).__le__(0.1 + 1e-10)`` to be called. But instead, ``approx(0.1).__lt__(0.1 + 1e-10)`` is used to comparison. This is because the call hierarchy of rich comparisons follows a fixed behavior. More information: :py:meth:`object.__ge__` .. versionchanged:: 3.7.1 ``approx`` raises ``TypeError`` when it encounters a dict value or sequence element of non-numeric type. .. versionchanged:: 6.1.0 ``approx`` falls back to strict equality for non-numeric types instead of raising ``TypeError``. Tz:pytest.approx() only supports ordered sequences, but got: )rRrrSrr_is_numpy_arrayrrqrnrrr\bytesrrT)r$r.r-r/r2clsrs r'approxr2s`(G$ - Hg &  ""8, 8 $  Hj )*XsU{2SJ8,Wn xc6 **r4crt|dxr*t|txrt|ttz S)N __getitem__)hasattrrRrr\r)r$s r'rnrns6-( 2 x ' 28S5[1 1r4ct|duS)zr Return true if the given object is implicitly convertible to ndarray, and numpy is already imported. N)r)objs r'rrs 3 t ++r4ctjjd}|S|j|ryt ||j r|St |ds t ddr|j|Sy)z Return an ndarray if the given object is implicitly convertible to ndarray, and numpy is already imported, otherwise None. rN __array__r__array_interface__)rrrrrRr rr)rrs r'rr&sb kkoog&B ~ ;;s  RZZ (J S+ &'%9N*O::c? " r4)robjectrzSequence[tuple[str, str, str]]rintrzSequence[object]rrrrrYr]rW)r/rXrYr*)r$rrYrX)rrrYrX)rrrYzndarray | None) __future__rcollections.abcrrrrdecimalrrnumbersr rrtypingr r rr r(r*rirqrrrTrSrrnrrr<r4r'rs"&#$!   0$     :8K8K@e@*e@PUWJUWpFUFURO;:O;d/L/4a+H,r4