Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
86
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
21
Setting up your Python environment for ML
pnijhara
0
39
Other Decks in Programming
See All in Programming
AIコーディングエージェント(Manus)
kondai24
0
170
Socio-Technical Evolution: Growing an Architecture and Its Organization for Fast Flow
cer
PRO
0
320
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
8
5.5k
Microservices Platforms: When Team Topologies Meets Microservices Patterns
cer
PRO
1
1k
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
5
2k
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
140
TUIライブラリつくってみた / i-just-make-TUI-library
kazto
1
370
TypeScript 5.9 で使えるようになった import defer でパフォーマンス最適化を実現する
bicstone
1
1.3k
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
130
Rediscover the Console - SymfonyCon Amsterdam 2025
chalasr
2
160
手軽に積ん読を増やすには?/読みたい本と付き合うには?
o0h
PRO
1
170
Integrating WordPress and Symfony
alexandresalome
0
150
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
Code Review Best Practice
trishagee
74
19k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Docker and Python
trallard
47
3.7k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Git: the NoSQL Database
bkeepers
PRO
432
66k
KATA
mclloyd
PRO
32
15k
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