Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pycon_Africa.pdf
Search
Prajjwal Nijhara
August 06, 2020
Programming
1
88
Pycon_Africa.pdf
Where and How to use Asserts in your Python code!
Best Practices!
Prajjwal Nijhara
August 06, 2020
Tweet
Share
More Decks by Prajjwal Nijhara
See All by Prajjwal Nijhara
Raft_Consensus_Algorithm_Prajjwal.pdf
pnijhara
0
22
Setting up your Python environment for ML
pnijhara
0
39
Other Decks in Programming
See All in Programming
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.4k
AgentCoreとHuman in the Loop
har1101
5
240
dchart: charts from deck markup
ajstarks
3
990
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
180
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
7
2.4k
Apache Iceberg V3 and migration to V3
tomtanaka
0
160
組織で育むオブザーバビリティ
ryota_hnk
0
180
AtCoder Conference 2025
shindannin
0
1.1k
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
980
Featured
See All Featured
The Curious Case for Waylosing
cassininazir
0
240
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
180
Designing Powerful Visuals for Engaging Learning
tmiket
0
230
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
1
53
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Tell your own story through comics
letsgokoyo
1
810
We Are The Robots
honzajavorek
0
160
Building Adaptive Systems
keathley
44
2.9k
WENDY [Excerpt]
tessaabrams
9
36k
Transcript
Where and How to use Asserts in your python code
~Prajjwal Nijhara Twitter: @NijharaPrajjwal Github: @pnijhara Blogs: https://pnijhara.github.io
The assert statement exists in almost every programming language. It
helps detect problems early in your program, where the cause is clear, rather than later as a side-effect of some other operation.
How Asserts work?
Assert Equivalent to
Example
Output:
Then where is the problem?
Python’s -O (optimize) flag
During this, these basic optimizations are done: • All the
assert statements are removed • All docstrings are removed (when -OO is selected) • Value of the __debug__ built-in variable is set to False What goes down?
Let us test the above code with -O flag
What can go wrong? If assert statements are used for
user-facing validation in production code, the block won’t be executed at all — potentially opening up a security vulnerability.
Look how syrusakbary validates mail
Fix! It is recommended to use Asserts only in tests.
Issue in Mopidy
Fix!
~Prajjwal Nijhara Twitter: @NijharaPrajjwal Github: @pnijhara Blogs: https://pnijhara.github.io Thank You!
References - https://discuss.deepsource.io/t/using-assert-outside-tests/79 - https://deepsource.io/blog/python-common-mistakes/ - https://github.com/syrusakbary/validate_email/blob/master/validate_email.py#L127 - https://github.com/mopidy/mopidy/issues/1903 -
https://github.com/mopidy/mopidy/pull/1904/files - https://deepsource.io