From 306145611d783d05fe7e6a1e7b324d9b5a1814ec Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Sat, 17 May 2025 10:11:05 -0700 Subject: [PATCH 1/3] Mention in the Any documentation how object is preferable This implements a suggestion in https://github.com/python/mypy/issues/9153#issuecomment-1837446187, which I thought was a good idea. --- docs/source/dynamic_typing.rst | 3 ++- docs/source/kinds_of_types.rst | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/source/dynamic_typing.rst b/docs/source/dynamic_typing.rst index 304e25c085a8..da40142d377d 100644 --- a/docs/source/dynamic_typing.rst +++ b/docs/source/dynamic_typing.rst @@ -1,6 +1,5 @@ .. _dynamic-typing: - Dynamically typed code ====================== @@ -94,6 +93,8 @@ third party libraries that mypy does not know about. This is particularly the ca when using the :option:`--ignore-missing-imports ` flag. See :ref:`fix-missing-imports` for more information about this. +.. _any-vs-object: + Any vs. object -------------- diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index 54693cddf953..b939d4471959 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -39,7 +39,10 @@ A value with the ``Any`` type is dynamically typed. Mypy doesn't know anything about the possible runtime types of such value. Any operations are permitted on the value, and the operations are only checked at runtime. You can use ``Any`` as an "escape hatch" when you can't use -a more precise type for some reason. +a more precise type for some reason. This should not be confused with the +:py:class:`object` type, which also represents any value, but in a type-safe +way; it's almost always preferable to use ``object`` instead of ``Any``, if +you can do so without great difficulty — see :ref:`any-vs-object` for more. ``Any`` is compatible with every other type, and vice versa. You can freely assign a value of type ``Any`` to a variable with a more precise type: From 637135b4d801985afc3af8f1191f3f7dcd3d4ec5 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 18 Jun 2025 11:05:03 -0700 Subject: [PATCH 2/3] Update docs/source/kinds_of_types.rst --- docs/source/kinds_of_types.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index b939d4471959..a3fa3e514d37 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -39,7 +39,9 @@ A value with the ``Any`` type is dynamically typed. Mypy doesn't know anything about the possible runtime types of such value. Any operations are permitted on the value, and the operations are only checked at runtime. You can use ``Any`` as an "escape hatch" when you can't use -a more precise type for some reason. This should not be confused with the +a more precise type for some reason. + +This should not be confused with the :py:class:`object` type, which also represents any value, but in a type-safe way; it's almost always preferable to use ``object`` instead of ``Any``, if you can do so without great difficulty — see :ref:`any-vs-object` for more. From 0211ce1e5cd0341cfb1d58de27426a986e16c775 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sat, 2 Aug 2025 14:54:18 -0700 Subject: [PATCH 3/3] Update docs/source/kinds_of_types.rst --- docs/source/kinds_of_types.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index a3fa3e514d37..8e721c0fb321 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -42,9 +42,9 @@ at runtime. You can use ``Any`` as an "escape hatch" when you can't use a more precise type for some reason. This should not be confused with the -:py:class:`object` type, which also represents any value, but in a type-safe -way; it's almost always preferable to use ``object`` instead of ``Any``, if -you can do so without great difficulty — see :ref:`any-vs-object` for more. +:py:class:`object` type, which represents the set of all values. +Unlike ``object``, ``Any`` introduces type unsafety — see +:ref:`any-vs-object` for more. ``Any`` is compatible with every other type, and vice versa. You can freely assign a value of type ``Any`` to a variable with a more precise type: