r/Python • • Jul 12 '26

Discussion Will PEP 505 ever be accepted?

https://peps.python.org/pep-0505/

I don't understand how null safe operators are less like plain English than other implemented features like the walrus operator.

In my opinion, the member access operator would make python significantly easier to read and understand.

Here's an example:

f = foo()

if f is None:
    baz = ""
else:
    baz = f.bar()
baz = foo()?.bar() ?: ""

EDIT: I forgot that "and" and "or" can be sometimes used in place of "?." and "?:" if the left value is not False, '', 0, [], or {}. It's a very implicit null check and has a lot of unexpected behavior.

17 Upvotes

192 comments sorted by

View all comments

Show parent comment

1

u/k0pernikus Jul 14 '26 edited Jul 14 '26

Perfect example why the ?. hides away the code-smell. The solution to your boilerplate isn't the null coalesence, it's proper parsing and strict type handling:

from typing import Any
from pydantic import BaseModel, ValidationError
import logging

class GPS(BaseModel):
    latitude: float
    longitude: float

class Address(BaseModel):
    street: str
    city: str
    gps: GPS

class Company(BaseModel):
    name: str
    headquarters: Address

class Profile(BaseModel):
    company: Company

class User(BaseModel):
    profile: Profile

raw_user_data: Any = {
    "profile": {
        "company": {
            "name": "OpenAI",
            "headquarters": {
                "street": "1 AI Plaza",
                "city": "San Francisco"
            }
        }
    }
}

latitude: float | str

try:
    user: User = User.model_validate(raw_user_data)
    latitude = user.profile.company.headquarters.gps.latitude
except ValidationError as e:
    latitude = ""
    for error in e.errors():
        failed_path: str = ".".join(str(loc) for loc in error['loc'])
        logging.error(f"Validation failed at: {failed_path} - {error['msg']}")

I can default to emtpy string AND still know exactly WHAT in my parser failed. (And yes, this example is a bit lacking as an empty street should still make the latitude parseable. Yet one can handle that case as well.) This can then be logged in sentry or kibana or whatever you have, trigger an alert, and I am fixing a bug long before any user even manages to file the bug report.

I parse tainted sources into trusted domain objects removable nullable types accordingly. (Yes, null still exist. Yes, I must handle it. Yet for that I can still composite their relevant ValueObjects, some of which may even carry the None through)

That's where the magic happens. Not in letting null values exist implicitly.

1

u/JanEric1 Jul 14 '26

Can you pleasse use proper code formatting. This is unreadable.

This is only a code smell if it is unexpected that this data is missing.

But it often isnt. If it is, you use a pydantic model wwith required fields and then you can use this approach. If it isnt, then you would use real optionals and null coalescing attribute access.

0

u/k0pernikus Jul 14 '26

No, I won't be using optionals. I will be rasing errors on None and treat them as exceptions rather than to magically convert them into a random default that I later am confused by.

For me, none or not expected results. (In some cases, you must even reason about None, undefined, and empty; it is a mess.) Yet ?. basically makes the universal claim than None values are just like valid data, and this just is not the case. This is what Type Narrowing was invented for.