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
95
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Pycon_Africa.pdf
Where and How to use Asserts in your Python code!
Best Practices!
Prajjwal Nijhara
August 06, 2020
More Decks by Prajjwal Nijhara
See All by Prajjwal Nijhara
Raft_Consensus_Algorithm_Prajjwal.pdf
pnijhara
0
30
Setting up your Python environment for ML
pnijhara
0
50
Other Decks in Programming
See All in Programming
[DroidKaigi 26] How We Moved from Android Studio to Slack
thisaay
0
190
Flow は今どうなっているか
mizdra
PRO
0
890
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
810
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
620
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
620
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.4k
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
160
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
960
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.7k
My Marp Sample
sinoue0108
0
140
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
120
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1.7k
Featured
See All Featured
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
590
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
810
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
660
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
Technical Leadership for Architectural Decision Making
baasie
3
550
Building Flexible Design Systems
yeseniaperezcruz
330
41k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.6k
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