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
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
20
Setting up your Python environment for ML
pnijhara
0
37
Other Decks in Programming
See All in Programming
そのAPI、誰のため? Androidライブラリ設計における利用者目線の実践テクニック
mkeeda
2
260
Testing Trophyは叫ばない
toms74209200
0
840
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
280
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
7
1.1k
🔨 小さなビルドシステムを作る
momeemt
3
670
print("Hello, World")
eddie
1
520
Amazon RDS 向けに提供されている MCP Server と仕組みを調べてみた/jawsug-okayama-2025-aurora-mcp
takahashiikki
1
110
AIでLINEスタンプを作ってみた
eycjur
1
230
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
440
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
2
720
サーバーサイドのビルド時間87倍高速化
plaidtech
PRO
0
720
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
810
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
A better future with KSS
kneath
239
17k
Agile that works and the tools we love
rasmusluckow
330
21k
Site-Speed That Sticks
csswizardry
10
810
Code Reviewing Like a Champion
maltzj
525
40k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
51
5.6k
Visualization
eitanlees
148
16k
4 Signs Your Business is Dying
shpigford
184
22k
Facilitating Awesome Meetings
lara
55
6.5k
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