🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
For this reason, tuples are special-cased in Python’s typing system. tuple accepts any number of type arguments: # OK: ``x`` is assigned to a tuple of length 1 where the sole element is an int x: tuple[int] = (5,) # OK: ``y`` is assigned to a tuple of length 2; # element 1 is an int, element 2 is a str y: tuple[int, str] = (5, "foo") # Error: the type annotation indicates a tuple of length 1, # but ``z`` has been assigned to a tuple of length 3 z: tuple[int] = (1, 2, 3)
🌐
DEV Community
dev.to › etenil › why-i-stay-away-from-python-type-annotations-2041
Why I stay away from Python type annotations - DEV Community
June 22, 2020 - With annotations, our code is "specific" by default and we work hard to make it generic. Note the quotes around "specific" as this is only enforced by linting tools like mypy and so you still need to do your pre-flight checks. This is a fundamental shift in the nature of the language. A lot of developers like to claim that type hints are the best thing for the language since sliced bread. However I get the feeling those people only look at Python in a vacuum.
Discussions

What is the point of type hinting when Python doesn't even respect it!?
because its a hint More on reddit.com
🌐 r/learnpython
26
0
August 15, 2024
is it a good practice to use type annotation on nearly everything that is a variable ?
If that's the type annotation it's probably easier to just accept that its type is better modeled as Unpredictable [PileOfJunk] More on reddit.com
🌐 r/learnpython
10
2
May 5, 2023
How am I supposed to do type annotation in Python when it's all Any type?
Python very much cares about types, it is a strongly typed language but is dynamically typed. Admittedly this can cause problems in substantial code bases which is why some large companies have put work into typing, notably Facebook and Dropbox. It is worth keeping in mind that variables don't have type as they do not hold any values, only memory references (pointers, if you prefer) to Python objects and it is the latter that has types. Similarly container objects, like lists, are collections of memory references. Some libraries are just lazily written and others explicitly intended to take full advantage of the dynamic typing nature of Python. Regarding beautifulsoup, you are not the only one trying to wrestle with it for type checking. For example, https://skeptric.com/typing-beautiful-soup/ More on reddit.com
🌐 r/learnpython
9
4
May 28, 2021
How many Python core developers use type annotations?
I use them as though they’re required as they help readability so much More on reddit.com
🌐 r/Python
121
169
November 14, 2023
🌐
Python
typing.python.org › en › latest › spec › annotations.html
Type annotations — typing documentation
Generally, an annotation expression is a type expression, optionally surrounded by one or more type qualifiers or by Annotated. Each type qualifier is valid only in some contexts. Note that while annotation expressions are the only expressions valid as type annotations in the type system, the Python language itself makes no such restriction: any expression is allowed.
🌐
CodiLime
codilime.com › blog › software development › backend › give python more speed with type annotations
Give Python more speed with type annotations
August 26, 2024 - In addition to improving code readability, type annotations can also help reduce bugs in your Python projects. By explicitly stating the expected data types for variables and function parameters, you're less likely to accidentally assign a value of the wrong type or pass an incorrect argument to a function.
🌐
YouTube
youtube.com › indently
Python: A Quick Guide To Type Annotations (ft. Mypy) - YouTube
In today’s video we will be learning about type annotations in Python, and also why I always try to type everything with no exceptions. ▶ Become job-ready wi...
Published: March 21, 2024
Views: 33K
🌐
Python Course
python-course.eu › annotations › type-annotations-and-hints.php
1. Type Annotations And Hints | Annotations | python-course.eu
July 13, 2023 - We offer live Python training courses covering the content of this site. ... Type annotations for variables in Python are a way to provide explicit type information about the expected type of a variable. They can help improve code readability, provide documentation, and enable static type checking ...
🌐
Florimond Manca
florimond.dev › en › posts › 2018 › 07 › why-i-started-using-python-type-annotations-and-why-you-should-too
Why I started using Python type annotations – and why you should too - Florimond Manca
July 27, 2018 - However for simple functions, using a full-blown docstring just to describe arguments and return values sometimes felt like a workaround — for the fact that Python did not offer any notion of type hinting whatsoever (or so I thought). Type annotations can sometimes replace a docstring altogether as they are — in my opinion — a very clean and simple way of documenting inputs and outputs.
Find elsewhere
🌐
CodeSignal
codesignal.com › learn › courses › advanced-functional-programming-techniques-2 › lessons › introduction-to-type-annotations-in-python
Introduction to Type Annotations in Python
Type annotations explicitly specify the data types of variables, function parameters, and return values. While Python is dynamically typed, meaning it doesn't require explicit data types, adding type annotations is beneficial:
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
Python has support for optional "type hints" (also called "type annotations").
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Functions › TypeAnnotations.html
12.7. Type Annotations — Foundations of Python Programming
Python allows you to indicate the intended type of the function parameters and the type of the function return value in a function definition using a special notation demonstrated in this example: This definition of duplicate makes use of type annotations that indicate the function’s parameter type and return type.
🌐
Medium
medium.com › @ramanbazhanau › advanced-type-annotations-in-python-part-2-58e0b756aede
Advanced Type Annotations in Python | Medium | Medium
September 19, 2023 - Dive deep into Python's advanced type annotations like Protocol, ParamSpec, and Annotated. Explore their applications, benefits, and best practices to write clear, robust Python code
🌐
University of Toronto
teach.cs.toronto.edu › ~csc148h › notes › python-recap › type_annotations.html
1.5 Python Type Annotations — CSC148 Course Notes
Since we need to be aware of the types we are using at any point in our code, it is good practise to document this. In this course, we will document the types of all functions and class instance attributes. We’ll use Python’s type annotation syntax to do so.
🌐
Reddit
reddit.com › r/learnpython › what is the point of type hinting when python doesn't even respect it!?
r/learnpython on Reddit: What is the point of type hinting when Python doesn't even respect it!?
August 15, 2024 -

So infuriating because I feel like my function is lying to me when it ends up letting other objects through the parameter.

Now every function I created needs an instance check which is unintuitive, verbose, and easily forgotten.

def wtf(string: str, integer:int):
   return 'TYPE HINT DOESNT DO ANYTHING' 

print( wtf( 123, 'wtf')  )
>> 'TYPE HINT DOESNT DO ANYTHING'

EDIT:

Turns out it is a noob mistake. Type hint only functions as a signal for IDE, but developers get to do whatever they want to your parameter.

If you really must enforce it, you got to have the line

if not isinstance(string, str): 
  raise TypeError()

Still, I don't like that this behavior isn't taught until it happens. So much time wasted on debugging production code when you take for granted your function is accepting only restricted data when it doesn't.

🌐
LogRocket
blog.logrocket.com › home › understanding type annotation in python
Understanding type annotation in Python - LogRocket Blog
June 4, 2024 - This tutorial will explore type hints and how you can add them to your Python code. It will focus on the mypy static type-checking tool and its operations in your code. You’ll learn how to annotate variables, functions, lists, dictionaries, and tuples.
🌐
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 2.3.1 documentation
# Another option is to just put the type in quotes def f(foo: 'A') -> int: # Also ok ... class A: # This can also come up if you need to reference a class in a type # annotation inside the definition of that class @classmethod def create(cls) -> A: ... See Class name forward references for more details. Decorator functions can be expressed via generics. See Declaring decorators for more details. Example using Python 3.12 syntax:
🌐
Reddit
reddit.com › r/learnpython › how am i supposed to do type annotation in python when it's all any type?
r/learnpython on Reddit: How am I supposed to do type annotation in Python when it's all Any type?
May 28, 2021 -

So for example, I am using BeautifulSoup library. And unlike other statically typed languages, no one seems to care about types in Python so the documentation does not tell me what's the return type of a function.

from bs4 import BeautifulSoup

soup =  BeautifulSoup(self.driver.page_source, "html.parser")
result = soup.find("pre").text

I use VSCode as my IDE, and when I hover over variables soup and result, it says unknown. So how am I supposed to annotate those types?

🌐
Reddit
reddit.com › r/python › how many python core developers use type annotations?
r/Python on Reddit: How many Python core developers use type annotations?
November 14, 2023 - Why shouldn't they use python? ... I think even with type annotations it's faster to write than c# and java. It's just less verbose with nice language features.
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-type-hinting-and-annotations-work-319d952247a6
How Python’s Type Hinting and Annotations Work | Medium
July 14, 2024 - This change has brought Python closer to statically typed languages like Java and C++, without sacrificing its dynamic nature. In its simplest form, type hinting uses annotations to specify the expected types of function parameters and return values.