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
82
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
8
Setting up your Python environment for ML
pnijhara
0
34
Other Decks in Programming
See All in Programming
slow types ってなんだろう?
karad
0
210
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
10
1.9k
Native Federation: The Future of Micro Frontends in Angular
manfredsteyer
PRO
0
170
Timeline エディター拡張入門
yucchiy
0
450
SIMD Parallel Programming with the Vector API
josepaumard
0
250
The Design of Everyday APIs - PyCon 2024
roguelynn
0
150
“Seeing Like a Programmer”—Resiliency, Limits, and Moral Hazards in Software Engineering (LambdaConf 2024)
chriskrycho
0
430
教えて!スクラムコーチ品質とスピードのバランスはどうすりゃいいの?
pinboro
0
100
CREってこういうこと? 体験入社 - 提案資料 - / what-is-cre-trial-employment
shinden
1
620
Adding Security to Microcontroller Ruby
sylph01
0
150
RailsConf 2024: Riffing on Rails: sketch your way to better designed code
kaspth
0
210
freeeのエンジニアが 就活で出そうな コーディングテストを 解説してみる
freee
0
150
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Making the Leap to Tech Lead
cromwellryan
125
8.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
228
130k
Faster Mobile Websites
deanohume
300
30k
A better future with KSS
kneath
231
16k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
0
120
Adopting Sorbet at Scale
ufuk
69
8.6k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
41
4.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
8
3.5k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
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