If that's the type annotation it's probably easier to just accept that its type is better modeled as Unpredictable [PileOfJunk] Answer from Mirage2k on reddit.com
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
To denote a tuple which could be of any length, and in which all elements are of the same type T, use the literal ellipsis ...: tuple[T, ...]. To denote an empty tuple, use tuple[()]. Using plain tuple as an annotation is equivalent to using ...
Videos
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 1.19.1 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections on Python ...
Python
typing.python.org › en › latest › spec › annotations.html
Type annotations — typing documentation
Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that __init__ ought to have a return annotation; the default behavior is thus the same as for other methods.) A type checker is expected to check the body of a checked function for consistency with the given annotations.
Reddit
reddit.com › r/learnpython › is it a good practice to use type annotation on nearly everything that is a variable ?
r/learnpython on Reddit: is it a good practice to use type annotation on nearly everything that is a variable ?
May 5, 2023 -
is this for ex considered a good practice or not "list[tuple[Callable[[str], str],Callable[[str], str]]]" ?
Top answer 1 of 4
5
If that's the type annotation it's probably easier to just accept that its type is better modeled as Unpredictable [PileOfJunk]
2 of 4
3
Yeahhhhhh... I struggle with this as well. For deeply nested objects, the value of detailed annotations yields diminishing returns IMHO. Just one man's opinion, but I don't think there's a single best answer here. I think you just need to use your judgment, and recall that the entire purpose of type hinting (in Python anyway) is to help other programmers (or future you) read your code. So if you put together a giant annotation like that, and then you step back and say "Whoa, wtf is that?", then it's probably not helping anyone much. In such scenarios, an alternative could be something like relying on generic types like Any as stand-ins for "lots of stuff of various types". Or if it's helpful, you could define your own type and give it a readable name. As a contrived example, something like: IterableOfFunctionObjects = list[tuple[Callable[[str], str],Callable[[str], str]]] ... foo: IterableOfFunctionObjects where obviously you'd use a more informative variable name than IterableOfFunctionObjects.
Runestone Academy
runestone.academy › ns › books › published › fopp › Functions › TypeAnnotations.html
12.7. Type Annotations — Foundations of Python Programming
Actually, this code should work just fine if the inputs are either integers or floats. If any are floats, then the return value will be a float. The more recent versions of type annotations in python allow the use the | symbol (pronounced “pipe”) to specify a union, that either of two types is permitted.
LogRocket
blog.logrocket.com › home › understanding type annotation in python
Understanding type annotation in Python - LogRocket Blog
June 4, 2024 - Documenting your code — anyone who wants to use an annotated function will know the type of parameters it accepts and the return value type at a glance · Additionally, IDEs understand your code much better and offer good autocompletion suggestions · Static typing in Python is optional and can be introduced gradually (this is known as gradual typing).
Python
typing.python.org › en › latest › reference › best_practices.html
Typing Best Practices — typing documentation
If a function accepts every possible object as an argument, for example because it’s only passed to str(), use object instead of Any as type annotation:
Python
peps.python.org › pep-0526
PEP 526 – Syntax for Variable Annotations | peps.python.org
Note that, although the syntax does allow tuple packing, it does not allow one to annotate the types of variables when tuple unpacking is used: # Tuple packing with variable annotation syntax t: Tuple[int, ...] = (1, 2, 3) # or t: Tuple[int, ...] = 1, 2, 3 # This only works in Python 3.8+ # Tuple unpacking with variable annotation syntax header: str kind: int body: Optional[List[str]] header, kind, body = message
GitHub
github.com › python-attrs › attrs › issues › 582
Best Practice in Python 3 for Type Annotation? · Issue #582 · python-attrs/attrs
October 6, 2019 - @attr.s class A: foo = attr.ib(type=int, init=False, default=10) vs: @attr.s(auto_attribs=True) class B: foo: int = attr.ib(init=False, default=10) The latter may be the best practice? Because IDEs are mostly written to understand Python...
Published Oct 06, 2019
Python
typing.python.org › en › latest › spec › special-types.html
Special types in annotations — typing documentation
Python’s numeric types complex, float and int are not subtypes of each other, but to support common use cases, the type system contains a straightforward shortcut: when an argument is annotated as having type float, an argument of type int is acceptable; similar, for an argument annotated as having type complex, arguments of type float or int are acceptable.
University of Toronto
teach.cs.toronto.edu › ~csc148h › notes › python-recap › type_annotations.html
1.5 Python Type Annotations — CSC148 Course Notes
Remember that we use type annotations as a form of communication, to tell other programmers how to use our function or class. With this goal in mind, we should always prefer giving specific type annotations to convey the most information possible, and only use Any when absolutely necessary. Note: ...
docs
missourimrr.github.io › docs › python › type-annotations › full-guide.html
Python Type Annotations Full Guide | docs
For the same reason as union types, optional types should be only used after its exact type has been resolved at runtime. As a best practice, this means utilizing Python’s is operator instead of the == operator to check for identity instead of equality. See example below. See PEP 526 - Syntax for Variable Type Annotations for more info.
Python Course
python-course.eu › annotations › type-annotations-and-hints.php
1. Type Annotations And Hints | Annotations | python-course.eu
Here's how you can use type annotations for variables: %%writefile example.py firstname: str firstname = 'David' surname: str = 'Miller' min_value: int = 0 max_value: int = 100 temperature: float = 17.9 ... As we have mentioned before: It's important to note that type annotations for variables in Python are optional and do not enforce the type at runtime.
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 - Okay that's slightly better. The secret sauce here was just to improve our naming to make the function's role more explicit -- a best practice. Note that to get rid of the lengthy type annotation we had to define a new class in the bottom option. This is the recommended way I've found.
DEV Community
dev.to › dan_starner › using-pythons-type-annotations-4cfe
Using Python's Type Annotations - DEV Community
January 11, 2022 - Type Annotating is a new feature added in PEP 484 that allows adding type hints to variables. They are used to inform someone reading the code what the type of a variable should be expected. This hinting brings a sense of statically typed control to the dynamically typed Python. This is accomplished by adding a given type declaration after initializing/declaring a variable or method. A helpful feature of statically typed languages is that the value of a variable can always be known within a specific domain.