MCPcopy Index your code
hub / github.com/RustPython/RustPython / _coerce

Function _coerce

Lib/statistics.py:1565–1594  ·  view source on GitHub ↗

Coerce types T and S to a common type, or raise TypeError. Coercion rules are currently an implementation detail. See the CoerceTest test class in test_statistics for details.

(T, S)

Source from the content-addressed store, hash-verified

1563
1564
1565def _coerce(T, S):
1566 """Coerce types T and S to a common type, or raise TypeError.
1567
1568 Coercion rules are currently an implementation detail. See the CoerceTest
1569 test class in test_statistics for details.
1570
1571 """
1572 # See http://bugs.python.org/issue24068.
1573 assert T is not bool, "initial type T is bool"
1574 # If the types are the same, no need to coerce anything. Put this
1575 # first, so that the usual case (no coercion needed) happens as soon
1576 # as possible.
1577 if T is S: return T
1578 # Mixed int & other coerce to the other type.
1579 if S is int or S is bool: return T
1580 if T is int: return S
1581 # If one is a (strict) subclass of the other, coerce to the subclass.
1582 if issubclass(S, T): return S
1583 if issubclass(T, S): return T
1584 # Ints coerce to the other type.
1585 if issubclass(T, int): return S
1586 if issubclass(S, int): return T
1587 # Mixed fraction & float coerces to float (or float subclass).
1588 if issubclass(T, Fraction) and issubclass(S, float):
1589 return S
1590 if issubclass(T, float) and issubclass(S, Fraction):
1591 return T
1592 # Any other combination is disallowed.
1593 msg = "don't know how to coerce %s and %s"
1594 raise TypeError(msg % (T.__name__, S.__name__))
1595
1596
1597def _exact_ratio(x):

Callers

nothing calls this directly

Calls 1

issubclassFunction · 0.85

Tested by

no test coverage detected