Your second way is correct.

def foo(opts: dict = {}):
    pass

print(foo.__annotations__)

this outputs

{'opts': <class 'dict'>}

Although it is not explicitly mentioned in PEP 484, type hints are a specific use of function annotations, as outlined in PEP 3107. The syntax section clearly demonstrates that keyword arguments can be annotated in this manner.

I strongly advise against using mutable keyword arguments. More information here.

Answer from noɥʇʎԀʎzɐɹƆ on Stack Overflow
Discussions

Can I define both function's argument's default value and data type in python? - Stack Overflow
I want to write something like this. def func_name(arg1, arg2, arg3 = 3) #this defines default value for optional argument arg3 def func_name(arg1, arg2, arg3: int) #and this defines data type for More on stackoverflow.com
🌐 stackoverflow.com
Type hinting weirdness with "None" as default value when I need to default to class' initial value or get a value from user
Your IDE is correct. Mypy will permit a: int = None at lower levels of strictness, but it is not technically correct; int | None is correct. Why do you think it isn't? Your method expects either a string, or None. What it then does to that value inside the method is not relevant to what parameter types it is expecting. More on reddit.com
🌐 r/learnpython
9
5
March 29, 2023
Python Function Default Values
def defaults(foo : str = “Hello”, bar : str = “World”) -> None: print(f”{foo} {bar}”) I believe this is the run down defaults() >>> Hello World defaults(“foo”) >>> foo World defaults( “foo”, “bar”) >>> foo bar defaults(bar = “bar”) >>> Hello bar defaults(bar = “foo”, foo = “bar”) >>> bar foo #also unexpectedly defaults(1,2) >>> 1 2 defaults(foo = “foo”, “bar”) >>>ERROR kwargs must come after args #these can cause errors if not formatted correct only use when necessary defaults([“foo”, “bar”]) >>>> [“foo”, “bar”] World defaults(*[“foo”, “bar”]) >>>> foo bar _dict = {“bar” : “_bar”, “foo” : “_foo”} defaults(**_dict) >>>> _foo _bar If you want to check types def type_default(foo : str = “Hello”, bar : str = None) -> None: if bar: print(f”{foo} {bar}”) else: print(f”{foo} World”) This should check if you accidentally send an false statement (like None, any empty string, list or dictionary, or any number != 0) to it. As you see Nonetype prints “None” in Python, and empty string would print no character, an empty list [], {} but we check all of that here. I like the type hinting but realize that’s just a comment not a requirement in Python. More on reddit.com
🌐 r/learnpython
6
0
June 28, 2023
Today I re-learned: Python function default arguments are retained between executions
I had to find this out the hard way :/ P.s. I am a little rusty on python, but if you want a mutable default value (say a list) you can make the default None then inside the function you would reassign the variable if it's None. More on reddit.com
🌐 r/Python
170
391
January 20, 2023
🌐
LabEx
labex.io › tutorials › python-how-to-specify-default-parameter-types-438410
How to specify default parameter types | LabEx
This tutorial explores various ... Python's type hinting capabilities. In Python, default parameters provide a convenient way to specify default values for function arguments....
🌐
GeeksforGeeks
geeksforgeeks.org › python › default-arguments-in-python
Default arguments in Python - GeeksforGeeks
In the first call greet(), no argument is passed, so Python uses the default value "Guest". In the second call greet("Kate"), the default value is overridden with "Kate". def function_name(param1=value1, param2=value2, ...): # function body
Published: March 18, 2026
🌐
Boot.dev
boot.dev › lessons › 3963019a-a8ce-45c0-b988-3e6e4121aa36
Learn Python for Beginners: Default Values | Boot.dev
They both take two integers as arguments, health and armor. ... In the function signature, give the armor parameter a default value of 0.
🌐
W3Schools
w3schools.com › python › gloss_python_function_default_parameter.asp
Python Default Parameter Value
If we call the function without argument, it uses the default value: def my_function(country = "Norway"): print("I am from " + country) my_function("Sweden") my_function("India") my_function() my_function("Brazil") Try it Yourself » · Python Functions Tutorial Function Call a Function Function Arguments *args Keyword Arguments **kwargs Passing a List as an Argument Function Return Value The pass Statement i Functions Function Recursion ... Coding fundamentals as bite-sized lessons and challenges.
🌐
Python
peps.python.org › pep-0696
PEP 696 – Type Defaults for Type Parameters | peps.python.org
This allows for a value to be used again when the type parameter to a generic is missing but another type parameter is specified. To use another type parameter as a default the default and the type parameter must be the same type (a TypeVar’s ...
Find elsewhere
🌐
Like Geeks
likegeeks.com › home › python › type hinting in python: default values and optional types
Type Hinting in Python: Default Values and Optional Types
November 25, 2024 - You can combine return type annotations with default values for a clear function signature. def calculate_tax(amount: float = 100.0) -> float: return amount * 0.15 print(calculate_tax()) print(calculate_tax(200.0)) ... The function calculate_tax ...
🌐
Reddit
reddit.com › r/learnpython › type hinting weirdness with "none" as default value when i need to default to class' initial value or get a value from user
r/learnpython on Reddit: Type hinting weirdness with "None" as default value when I need to default to class' initial value or get a value from user
March 29, 2023 -

Sorry for the confusing title.

I have been adding some type hinting into my code and noticed that if I have something like:

def create(self, a: int = None) -> None
a = a or self.a

Where I would like to have the user either give a as an int or just skip it and the function will default to the class' initial value for a. This works just fine but this shows in the IDE as create(self, a: int | None = None) -> None which looks like None is a valid value for a, even though that is not what I am looking for.

Then if I have an artificial case like:

def create(self, a: int = 'test') -> None
...

Which then shows up as create(self, a: int = 'test') -> None which looks weird but at least it doesn't explicitly imply that str is a valid parameter.

So I guess two questions: what makes None special? And, is this the way to use type hinting when I need to default to class' initial value or get an argument from user?

🌐
Reddit
reddit.com › r/learnpython › python function default values
r/learnpython on Reddit: Python Function Default Values
June 28, 2023 -

Say I have a function defined like

def f(foo: str, bar: str | None = "world"):
  print(f"{foo} {bar})
s = None
f("hello") # prints "hello world"
f("hello", s) # prints "hello None"

Is there a data type (or concept) in Python that would enable the second function call to print "hello world" as well? In other words, bar should take on its default value. In javascript, passing undefined for bar would accomplish this. In Python, it seems like I would have to add a conditional to the function.

def f(foo: str, bar: str | None = "world"):
  bar = bar or "world"
  print(f"{foo} {bar})

This isn't a major issue for me, but when a function has a lot of optional arguments with default values, it can be pretty verbose (for Python standards) to add a conditional for every optional argument.

🌐
InformIT
informit.com › articles › article.aspx
5.2 Default Arguments | Python Functions | InformIT
As a general practice, to avoid such surprises, only use immutable objects for default argument values—numbers, strings, Booleans, None, and so on. ... I would like to receive exclusive offers and hear about products from InformIT and its family of brands.
🌐
Home and Learn
homeandlearn.uk › python-functions-defaults.html
Python Default function values
You can set up your Python functions to have default values. Between the round brackets of your function definition, come up with a variable name then type an equals sign.After the equals sign, type a value for your variable. For example, suppose we want our function from the previous lesson ...
🌐
Reddit
reddit.com › r/python › today i re-learned: python function default arguments are retained between executions
r/Python on Reddit: Today I re-learned: Python function default arguments are retained between executions
January 20, 2023 - No. strings are immutables in python. It's perfectly safe to write ... “stick to literals” is not good advice. List and dictionary literals would be problematic as default parameter values since those are mutable types. Ironically the example you’ve given here could be simplified by just using the string literal as the default since strings are immutable.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python default parameters
Python Default Parameters
October 5, 2020 - To specify default values for parameters, you use the following syntax: def function_name(param1, param2=value2, param3=value3, ...):Copy · In this syntax, you specify default values (value2, value3, …) for each parameter using the assignment operator (=). When you call a function and pass ...
🌐
Codecademy
codecademy.com › learn › learn-intermediate-python-3 › modules › int-python-function-arguments › cheatsheet
Learn Intermediate Python 3: Function Arguments Cheatsheet | Codecademy
In Python, a default parameter is defined with a fallback value as a default argument. Such parameters are optional during a function call. If no argument is provided, the default value is used, and if an argument is provided, it will overwrite ...
🌐
Real Python
realpython.com › python-optional-arguments
Using Python Optional Arguments When Defining Functions – Real Python
July 19, 2026 - This tutorial shows you how and why to use Python optional arguments, and how to avoid common pitfalls when setting defaults. By the end of this tutorial, you’ll understand that: Parameters are names in a function definition, while arguments are the values you pass when calling the function · You can assign default values to parameters so that arguments become optional · You should avoid mutable data types like lists or dictionaries as default values to prevent unexpected behavior
🌐
Programiz
programiz.com › python-programming › function-argument
Python Function Arguments (With Examples)
No value is passed during the function call. Hence, default value is used for both parameters a and b.
🌐
The Python Coding Book
thepythoncodingbook.com › home › blog › optional arguments with default values in python functions [intermediate python functions series #3]
Optional Arguments with Default Values in Python Functions
January 18, 2023 - When you call the function, the corresponding argument is optional. If the argument is not present in the function call, the default value is used · Next Article: Argh! What are args and kwargs in Python?
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 week ago - When called directly, this method ... evaluate the value in a different format. Added in version 3.14. ... The default value of the type variable, or typing.NoDefault if it has no default....