Sitemap

🐍 Python Gotcha: Why Optional Doesn’t Make Arguments Optional 🚫➡️✅

2 min readSep 2, 2025
Press enter or click to view image in full size
Photo by Marcus Urbenz on Unsplash

Ever written a Python function with Optional and wondered why the argument still isn’t optional? 🤯
This post clears up the confusion between type hints and default values with simple examples.

🤔 The Confusion

When people first see this in Python:

from typing import Optional

def greet(name: Optional[str]) -> None:
print("Hello", name)

The immediate thought is:
“Cool! Now I can call greet() without passing a parameter.”

But… nope. Try to call greet().

Wait — what? Didn’t we just say Optional? Shouldn’t it make the argument optional?

🔍 The Truth About Optional

Here’s the catch:

  • Optional[str] is just type hinting.
  • It tells Python (and your IDE/type checker like mypy) that the parameter can be either str or None.
  • It does not magically make the parameter optional in function calls.

So this works:

def greet(name: Optional[str]) -> None:
print("Hello", name)

greet("Alice")
greet(None)

#output
# Hello Alice
# Hello None

But skipping the argument entirely? ❌ Not allowed.

✅ How to Make It Truly Optional

If you want the argument to be skippable, you need to give it a default value:

def greet(name: Optional[str] = None) -> None:
if name is None:
print("Hello, stranger!")
else:
print("Hello", name)


greet()
greet("Alice")
greet(None)

# output
# ✅ Hello, stranger!
# ✅ Hello Alice
# ✅ Hello, stranger!

⚡ Quick Rule of Thumb

  • Optional[X] → means X or None (type hint only).
  • = None → makes it skippable at runtime.
  • Use both together if you want a truly optional parameter.

📝 Final Takeaway

Don’t let the name fool you — Optional isn’t about skipping arguments.
It’s about accepting None.

If you want your function parameter to be optional in practice, remember to also assign a default value.

Next time someone asks: “Why is Optional not optional?"

You can confidently say: “Because Python likes to troll us a little 😉”

--

--

No responses yet