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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
260
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
390
CSC307 Lecture 13
javiergs
PRO
0
320
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
370
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
380
Geminiの機能を調べ尽くしてみた
naruyoshimi
0
200
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
3
110
技術検証結果の整理と解析をAIに任せよう!
keisukeikeda
0
110
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
180
Cyrius ーLinux非依存にコンテナをネイティブ実行する専用OSー
n4mlz
0
120
Swift ConcurrencyでよりSwiftyに
yuukiw00w
0
250
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
790
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
4
250
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
320
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
190
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
170
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
410
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
200
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