None should be checked with is None in docs #7874
Replies: 5 comments
-
@GeorgeFischhof I wouldn't label it as a |
Beta Was this translation helpful? Give feedback.
-
PR #824 added |
Beta Was this translation helpful? Give feedback.
-
Hi @shifr yes, it is valid for strings, but singletons should be checked with is operator (as PEP8 says) and explicit is better than implicit ;-) And as you wrote too this check checks two thinks, but perhaps it is not the best for beginners |
Beta Was this translation helpful? Give feedback.
-
You're right about checking |
Beta Was this translation helpful? Give feedback.
-
Thanks for the interest @GeorgeFischhof , but as I commented on the PR, I would prefer to keep it as is. It's a bit more of a personal preference than best practices. In fact, using |
Beta Was this translation helpful? Give feedback.
-
Hi All
the library and the documentation are fantastic
I am a QA eng., and teach Python for my colleagues.
I saw in some examples that None is checked as it would be a boolean expression
for example:
https://fastapi.tiangolo.com/tutorial/body/
async def create_item(item_id: int, item: Item, q: str = None):
result = {"item_id": item_id, **item.dict()}
if q:
I saw that the documentation is written for beginners too.
I think checking the None as it would be a boolean expression in the docs encourages beginners to use this formula instead of checking with is operator. And it is not good, because not None will them result unexpected results.
These examples should be corrected.
BR,
George
Beta Was this translation helpful? Give feedback.
All reactions