Skip to content

Commit f5ea8a0

Browse files
committed
Test functools.singledispatch/functools.singledispatchmethod annotate functions
1 parent 4b955a8 commit f5ea8a0

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Lib/test/test_annotationlib.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,75 @@ def format(format, /, __Format=Format,
17191719

17201720
self.assertEqual(annotations, {"x": int, "y": str})
17211721

1722+
def test_callable_singledispatch_annotate_forwardref_value_fallback(self):
1723+
# If Format.STRING and Format.VALUE_WITH_FAKE_GLOBALS are not
1724+
# supported fall back to Format.VALUE and convert to strings
1725+
@functools.singledispatch
1726+
def format(format, /, __Format=Format,
1727+
__NotImplementedError=NotImplementedError):
1728+
if format == __Format.VALUE:
1729+
return {"x": str}
1730+
else:
1731+
raise __NotImplementedError(format)
1732+
1733+
@format.register(float)
1734+
def _(format, /, __Format=Format,
1735+
__NotImplementedError=NotImplementedError):
1736+
if format == __Format.VALUE:
1737+
return {"x": float}
1738+
else:
1739+
raise __NotImplementedError(format)
1740+
1741+
@format.register(int)
1742+
def _(format, /, __Format=Format,
1743+
__NotImplementedError=NotImplementedError):
1744+
if format == __Format.VALUE:
1745+
return {"x": int}
1746+
else:
1747+
raise __NotImplementedError(format)
1748+
1749+
annotations = annotationlib.call_annotate_function(
1750+
format,
1751+
Format.FORWARDREF,
1752+
)
1753+
1754+
self.assertEqual(annotations, {"x": int})
1755+
1756+
def test_callable_singledispatchmethod_annotate_forwardref_value_fallback(self):
1757+
# If Format.STRING and Format.VALUE_WITH_FAKE_GLOBALS are not
1758+
# supported fall back to Format.VALUE and convert to strings
1759+
class Annotate:
1760+
@functools.singledispatchmethod
1761+
def format(self, format, /, __Format=Format,
1762+
__NotImplementedError=NotImplementedError):
1763+
if format == __Format.VALUE:
1764+
return {"x": str}
1765+
else:
1766+
raise __NotImplementedError(format)
1767+
1768+
@format.register(float)
1769+
def _(self, format, /, __Format=Format,
1770+
__NotImplementedError=NotImplementedError):
1771+
if format == __Format.VALUE:
1772+
return {"x": float}
1773+
else:
1774+
raise __NotImplementedError(format)
1775+
1776+
@format.register(int)
1777+
def _(self, format, /, __Format=Format,
1778+
__NotImplementedError=NotImplementedError):
1779+
if format == __Format.VALUE:
1780+
return {"x": int}
1781+
else:
1782+
raise __NotImplementedError(format)
1783+
1784+
annotations = annotationlib.call_annotate_function(
1785+
Annotate().format,
1786+
Format.FORWARDREF,
1787+
)
1788+
1789+
self.assertEqual(annotations, {"x": int})
1790+
17221791
def test_callable_object_annotate_string_fakeglobals(self):
17231792
# If Format.STRING is not supported but Format.VALUE_WITH_FAKE_GLOBALS is
17241793
# prefer that over Format.VALUE

0 commit comments

Comments
 (0)