Python 3 type checking not working using input module?

Why doesn't type checking work in Python 3?

I made the following code with type checks or hints:

import typing

def hello(message: str):
    print(type(message))
    print(message)

hello('Hello!')
hello(1)
hello(1.1)

      

It produces valid output (but no errors in int or float).

<class 'str'>
Hello!
<class 'int'>
1
<class 'float'>
1.1

      

Why does it work like this? I may not understand the Python input and prompt module.

+4


source to share


5 answers


Python hint types are informational only. Type checking or automatic typecasting of arguments is not part of the language. See PEP 3107 :

Function annotations are nothing more than a way to associate arbitrary Python expressions with various parts of a function at compile time.

Type hints can be used by the plug-in to check the types of arguments and return values, or even to cast arguments to the expected type. For example, here is a module that will check the types of arguments and complain if it finds a mismatch.



But that's not how Python itself works, so don't rely on it or look for ways to incorporate it into your code. In Python style, your functions should be written as flexible as possible about the types of arguments they can handle (google "duck typing"). If they get something they can't handle ... well, that's what exceptions are for.

Update: The module that provides support for type hints has been added to the standard library ("temporarily") since Python 3.5. It provides some useful type names including , and , as well as a helper function . Typical hints remain very, very optional. typing

Any

Callable

Union

NewType

+5


source


Python 3 doesn't have the type of type checking that you are looking for.

def hello(message: str):

      

This is a function annotation.



https://www.python.org/dev/peps/pep-3107/

All it does is associate a bit of extra data with the function object. This can be verified later with an attribute func_annotations

on the function.

This has no built in behavior other than this. The goal is for third parties to be able to create behavior on top of this.

+3


source


The hint type is just a hint , it should tell users that the function is not expecting what it requires. This is explicitly mentioned in the PEP that introduced them: PEP 3107 :

Function annotation basics

Before we start discussing the exact ins and outs of Python 3.0 function annotations, let me first talk broadly about what annotations are and not:

  • Functional annotations, both for parameters and return values, are completely optional.

  • Function annotations are nothing more than a way to associate arbitrary Python expressions with various parts of a function at compile time.

    Python itself does not bind any particular meaning or meaning to an annotation. [...]

+2


source


What you want is the static typing

opposite of the paradigm dynamic typing

adopted by python.

You can use mypy , a project that puts static typing in python.

+2


source


They are called "Type Hints" for a reason. Python simply provides the ability to mark types in a standard and structured way to guide other programmers or make IDE error checking easier.

However, Python does not enforce these hints, and there is no plan, they are just like that, but nothing more than comments.

+1


source







All Articles