SlideShare a Scribd company logo
How To Delete Unused
Python Code Safely
Aug, 2018 by PCMan (洪任諭)
<pcman.tw@gmail.com>
COSCUP 2018
Who Am I?
● Free Software
○ Creator of LXDE/LXQt desktop
○ PCMan BBS client
○ Linux user since 2004
● Career
○ Senior System Engineer; Appier Inc.
○ Physician, Taipei Veterans General Hospital (Rheumatology)
● Education
○ Master of Computer Science, National Taiwan University
○ Medicine, National Yang-Ming University
Problems of Legacy Code
● Large and complicated code base
● It works! (but I don't know why)
● Unused variables
● Unused functions
● Unused imports
● Unreachable code path
● No documentations
● Broken unit tests
3
Static Code Analysis
4
Some Helpful Tools
● Pyflake + autoflake
○ Delete unused variables
○ Delete unused imports
○ Expand import *
○ Cannot delete unused functions
● Vulture
○ (Claim to) find unused functions
○ Provide confidence levels
○ Unfortunately, does not work in some cases 5
Example use of autoflake
> autoflake 
--in-place  # warning: this will directly edit the *.py files
--remove-unused-variables 
--remove-all-unused-imports 
--expand-star-imports 
--recursive 
<source dir>
6
Example use of vulture - Find Unused Functions
> vulture 
myscript.py 
--min-confidence 100 # Only report 100% dead code.
● Does not work for a some code base I tested and always
reported 60% confidence for all functions :-(
7
Example Output of Vulture 0.25
$ vulture --sort-by-size .
app.py:1: unused import 'sys' (90% confidence, 1 line)
app.py:2: unused import 'json' (90% confidence, 1 line)
app.py:5: unused import 'demo2' (90% confidence, 1 line)
app.py:10: unused variable 'i' (60% confidence, 1 line)
app.py:14: unused function 'unused_func0' (60% confidence, 2 lines)
demo.py:3: unused function 'unused_func1' (60% confidence, 2 lines)
demo2.py:1: unused function 'unused_func2' (60% confidence, 2 lines)
demo2.py:4: unused function 'unused_func3' (60% confidence, 2 lines)
demo2.py:7: unused function 'unused_func4' (60% confidence, 2 lines)
8Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
Code Coverage
9
Coverage.py
● Measure code coverage of Python programs
● Beautiful reports
● Often used to calculate unit test coverage
● Example:
> coverage run your_program.py
> coverage report # text-based summary report
> coverage html # generate colorful detailed reports
10
https://coverage.readthedocs.io/en/coverage-4.5.1a/
Example Output of Coverage.py
11Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
Example Output of Coverage.py
12Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
Code Coverage Tests
Pros:
● Very detailed reports (statement level)
● Can observe actual behavior at runtime
Cons:
● Need code that can run (not for broken legacy code)
● Need test cases with good quality and coverage
● May not work reliably with concurrency (such as gevent)
13
Alternatives?
14
DIY with Python AST
15
16
Example Python AST
17
<_ast.Module object at 0x7f3aeaecd630>
<_ast.ImportFrom object at 0x7f3aeaecd550>
alias(name='used_func4', asname=None)
<_ast.FunctionDef object at 0x7f3aeaecd668>
arguments(args=[], vararg=None, kwonlyargs=[],
kw_defaults=[], kwarg=None, defaults=[])
<_ast.Expr object at 0x7f3aeaecd748>
<_ast.Call object at 0x7f3aeaecd898>
<_ast.Name object at 0x7f3aeaecd860>
Load()
Str(s='unused 1')
# module demo.py
from demo2 
import used_func4
def unused_func1():
print("unused 1")
Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
Some Useful Python AST Node Types
18
● ast.Module: the root node
● ast.FunctionDef: function definition
● ast.Attribute: access attribute of an object
● ast.Name: symbol name
● ast.Call: method invocation
● ast.ImportFrom: from xxx import yyy
● ast.NodeVisitor: tree traversal
Reference: http://greentreesnakes.readthedocs.io/en/latest/tofrom.html
Find Functions and Their (Potential) Callers
19
def find_symbols(py_file):
used_symbols = defaultdict(int)
defined_funcs = []
with open(py_file) as f:
src = f.read()
tree = ast.parse(src, py_file) # parse the python code
for node in ast.walk(tree): # iterate through all nodes (unordered)
if isinstance(node, ast.FunctionDef): # function definition
defined_funcs.append(node.name)
elif isinstance(node, ast.Attribute): # reference obj.attribute
used_symbols[node.attr] += 1
elif isinstance(node, ast.Name): # name of an identifier
used_symbols[node.id] += 1
return defined_funcs, used_symbols
Find Unused Functions
20
1. Find all defined functions in the whole source tree
2. Find all references to object attributes or symbol names in
the whole source tree
3. A function is defined but the name is not referenced in all
files ⇒ unused
Example Output - List Unused Functions
> python3 ../find_unused.py ./*.py
./app.py
unused_func0
./demo.py
unused_func1
./demo2.py
unused_func2
unused_func3
unused_func4
21Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
Pitfalls & Limitations
22
● False negative (unused but not recognized)
○ Only compare symbol names
○ Different functions can have the same name
○ Unused recursive functions cannot be found
(referenced by itself → always used)
● Cannot handle dynamic cases:
○ getattr(obj, 'func_name')()
○ globals()['func_name']()
○ from module import * → use autoflake to remove this
The Whole Process
1. Remove unused import and expand * with autoflake
2. Examine every getattr(), globals(), and __import__ in the
code (to make sure they don't reference functions)
3. List unused functions
4. Delete unused functions (with an IDE like PyCharm)
5. Repeat steps 1 - 4 until all unused functions are deleted
a. After deleting some functions, their dependencies may
become unused as well
23
Get the Tool
https://github.com/PCMan/python-find-unused-func
24

More Related Content

PDF
Basic c++ 11/14 for python programmers
PPTX
Basic C++ 11/14 for Python Programmers
PPTX
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
Address/Thread/Memory Sanitizer
PPTX
Summary of C++17 features
PPTX
Hacking Go Compiler Internals / GoCon 2014 Autumn
PDF
Антон Бикинеев, Writing good std::future&lt; C++ >
Basic c++ 11/14 for python programmers
Basic C++ 11/14 for Python Programmers
Fun with Lambdas: C++14 Style (part 1)
Address/Thread/Memory Sanitizer
Summary of C++17 features
Hacking Go Compiler Internals / GoCon 2014 Autumn
Антон Бикинеев, Writing good std::future&lt; C++ >

What's hot (20)

PDF
C++ idioms by example (Nov 2008)
PDF
Writing good std::future&lt;c++>
PPTX
Алексей Кутумов, Вектор с нуля
PPTX
PPTX
C++ 11 Features
PDF
Конверсия управляемых языков в неуправляемые
PPTX
C++17 std::filesystem - Overview
ODP
Отладка в GDB
PDF
C++11 & C++14
PPTX
C++ via C#
PPTX
Fun with Lambdas: C++14 Style (part 2)
PDF
Kamil witecki asynchronous, yet readable, code
PDF
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
PPTX
Lambda Expressions in C++
PPTX
Let's talks about string operations in C++17
PPT
Csdfsadf
PPTX
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
PPT
Constructor,destructors cpp
C++ idioms by example (Nov 2008)
Writing good std::future&lt;c++>
Алексей Кутумов, Вектор с нуля
C++ 11 Features
Конверсия управляемых языков в неуправляемые
C++17 std::filesystem - Overview
Отладка в GDB
C++11 & C++14
C++ via C#
Fun with Lambdas: C++14 Style (part 2)
Kamil witecki asynchronous, yet readable, code
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Lambda Expressions in C++
Let's talks about string operations in C++17
Csdfsadf
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Constructor,destructors cpp
Ad

Similar to 2018 cosup-delete unused python code safely - english (20)

PDF
First Steps in Python Programming
DOCX
pythonlibrariesandmodules-210530042906.docx
PPTX
Python Libraries and Modules
PDF
PHYTON-REPORT.pdf
PPTX
Docketrun's Python Course for beginners.pptx
PPTX
libraries in python using different .pptx
PDF
Pemrograman Python untuk Pemula
PDF
Python for web security - beginner
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
PPTX
3-Tut2_Interfacing_Sensors_RPioT.pptx good reference
PPT
An Overview Of Python With Functional Programming
PDF
W-334535VBE242 Using Python Libraries.pdf
PDF
Functions and modules in python
PPTX
Functions2.pptx
PDF
Cluj.py Meetup: Extending Python in C
PDF
Interview-level-QA-on-Python-Programming.pdf
DOCX
Python Interview Questions For Experienced
PDF
web programming UNIT VIII python by Bhavsingh Maloth
PPTX
Python and You Series
PPTX
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
First Steps in Python Programming
pythonlibrariesandmodules-210530042906.docx
Python Libraries and Modules
PHYTON-REPORT.pdf
Docketrun's Python Course for beginners.pptx
libraries in python using different .pptx
Pemrograman Python untuk Pemula
Python for web security - beginner
Intro-to-Python-Part-1-first-part-edition.pdf
3-Tut2_Interfacing_Sensors_RPioT.pptx good reference
An Overview Of Python With Functional Programming
W-334535VBE242 Using Python Libraries.pdf
Functions and modules in python
Functions2.pptx
Cluj.py Meetup: Extending Python in C
Interview-level-QA-on-Python-Programming.pdf
Python Interview Questions For Experienced
web programming UNIT VIII python by Bhavsingh Maloth
Python and You Series
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
Ad

More from Jen Yee Hong (8)

PDF
COSCUP 2019 國際開放原始碼專案經營 - 從失敗中學習
PDF
PIME - 用 Python 和 JavaScript 快速開發 Windows 的中文輸入法
PDF
COSCUP 2016: 開源軟硬體實做全套電子鼓(python + RPi)
PDF
TPET8演講: 非典型程式教育
PDF
2016-04-07-清大-國際化開源專案技術實務與經驗分享
PDF
Py drum
PDF
PIME - 用 Python 快速開發 Windows 的中文輸入法 (COSCUP 2015)
PDF
Gtk to qt
COSCUP 2019 國際開放原始碼專案經營 - 從失敗中學習
PIME - 用 Python 和 JavaScript 快速開發 Windows 的中文輸入法
COSCUP 2016: 開源軟硬體實做全套電子鼓(python + RPi)
TPET8演講: 非典型程式教育
2016-04-07-清大-國際化開源專案技術實務與經驗分享
Py drum
PIME - 用 Python 快速開發 Windows 的中文輸入法 (COSCUP 2015)
Gtk to qt

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
L1 - Introduction to python Backend.pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPT
Introduction Database Management System for Course Database
PDF
Odoo Companies in India – Driving Business Transformation.pdf
Nekopoi APK 2025 free lastest update
Design an Analysis of Algorithms II-SECS-1021-03
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Understanding Forklifts - TECH EHS Solution
Wondershare Filmora 15 Crack With Activation Key [2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 41
VVF-Customer-Presentation2025-Ver1.9.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
How Creative Agencies Leverage Project Management Software.pdf
CHAPTER 2 - PM Management and IT Context
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
L1 - Introduction to python Backend.pptx
ManageIQ - Sprint 268 Review - Slide Deck
Design an Analysis of Algorithms I-SECS-1021-03
Introduction Database Management System for Course Database
Odoo Companies in India – Driving Business Transformation.pdf

2018 cosup-delete unused python code safely - english

  • 1. How To Delete Unused Python Code Safely Aug, 2018 by PCMan (洪任諭) <pcman.tw@gmail.com> COSCUP 2018
  • 2. Who Am I? ● Free Software ○ Creator of LXDE/LXQt desktop ○ PCMan BBS client ○ Linux user since 2004 ● Career ○ Senior System Engineer; Appier Inc. ○ Physician, Taipei Veterans General Hospital (Rheumatology) ● Education ○ Master of Computer Science, National Taiwan University ○ Medicine, National Yang-Ming University
  • 3. Problems of Legacy Code ● Large and complicated code base ● It works! (but I don't know why) ● Unused variables ● Unused functions ● Unused imports ● Unreachable code path ● No documentations ● Broken unit tests 3
  • 5. Some Helpful Tools ● Pyflake + autoflake ○ Delete unused variables ○ Delete unused imports ○ Expand import * ○ Cannot delete unused functions ● Vulture ○ (Claim to) find unused functions ○ Provide confidence levels ○ Unfortunately, does not work in some cases 5
  • 6. Example use of autoflake > autoflake --in-place # warning: this will directly edit the *.py files --remove-unused-variables --remove-all-unused-imports --expand-star-imports --recursive <source dir> 6
  • 7. Example use of vulture - Find Unused Functions > vulture myscript.py --min-confidence 100 # Only report 100% dead code. ● Does not work for a some code base I tested and always reported 60% confidence for all functions :-( 7
  • 8. Example Output of Vulture 0.25 $ vulture --sort-by-size . app.py:1: unused import 'sys' (90% confidence, 1 line) app.py:2: unused import 'json' (90% confidence, 1 line) app.py:5: unused import 'demo2' (90% confidence, 1 line) app.py:10: unused variable 'i' (60% confidence, 1 line) app.py:14: unused function 'unused_func0' (60% confidence, 2 lines) demo.py:3: unused function 'unused_func1' (60% confidence, 2 lines) demo2.py:1: unused function 'unused_func2' (60% confidence, 2 lines) demo2.py:4: unused function 'unused_func3' (60% confidence, 2 lines) demo2.py:7: unused function 'unused_func4' (60% confidence, 2 lines) 8Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
  • 10. Coverage.py ● Measure code coverage of Python programs ● Beautiful reports ● Often used to calculate unit test coverage ● Example: > coverage run your_program.py > coverage report # text-based summary report > coverage html # generate colorful detailed reports 10 https://coverage.readthedocs.io/en/coverage-4.5.1a/
  • 11. Example Output of Coverage.py 11Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
  • 12. Example Output of Coverage.py 12Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
  • 13. Code Coverage Tests Pros: ● Very detailed reports (statement level) ● Can observe actual behavior at runtime Cons: ● Need code that can run (not for broken legacy code) ● Need test cases with good quality and coverage ● May not work reliably with concurrency (such as gevent) 13
  • 15. DIY with Python AST 15
  • 16. 16
  • 17. Example Python AST 17 <_ast.Module object at 0x7f3aeaecd630> <_ast.ImportFrom object at 0x7f3aeaecd550> alias(name='used_func4', asname=None) <_ast.FunctionDef object at 0x7f3aeaecd668> arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]) <_ast.Expr object at 0x7f3aeaecd748> <_ast.Call object at 0x7f3aeaecd898> <_ast.Name object at 0x7f3aeaecd860> Load() Str(s='unused 1') # module demo.py from demo2 import used_func4 def unused_func1(): print("unused 1") Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
  • 18. Some Useful Python AST Node Types 18 ● ast.Module: the root node ● ast.FunctionDef: function definition ● ast.Attribute: access attribute of an object ● ast.Name: symbol name ● ast.Call: method invocation ● ast.ImportFrom: from xxx import yyy ● ast.NodeVisitor: tree traversal Reference: http://greentreesnakes.readthedocs.io/en/latest/tofrom.html
  • 19. Find Functions and Their (Potential) Callers 19 def find_symbols(py_file): used_symbols = defaultdict(int) defined_funcs = [] with open(py_file) as f: src = f.read() tree = ast.parse(src, py_file) # parse the python code for node in ast.walk(tree): # iterate through all nodes (unordered) if isinstance(node, ast.FunctionDef): # function definition defined_funcs.append(node.name) elif isinstance(node, ast.Attribute): # reference obj.attribute used_symbols[node.attr] += 1 elif isinstance(node, ast.Name): # name of an identifier used_symbols[node.id] += 1 return defined_funcs, used_symbols
  • 20. Find Unused Functions 20 1. Find all defined functions in the whole source tree 2. Find all references to object attributes or symbol names in the whole source tree 3. A function is defined but the name is not referenced in all files ⇒ unused
  • 21. Example Output - List Unused Functions > python3 ../find_unused.py ./*.py ./app.py unused_func0 ./demo.py unused_func1 ./demo2.py unused_func2 unused_func3 unused_func4 21Test example code: https://github.com/PCMan/python-find-unused-func/tree/master/example
  • 22. Pitfalls & Limitations 22 ● False negative (unused but not recognized) ○ Only compare symbol names ○ Different functions can have the same name ○ Unused recursive functions cannot be found (referenced by itself → always used) ● Cannot handle dynamic cases: ○ getattr(obj, 'func_name')() ○ globals()['func_name']() ○ from module import * → use autoflake to remove this
  • 23. The Whole Process 1. Remove unused import and expand * with autoflake 2. Examine every getattr(), globals(), and __import__ in the code (to make sure they don't reference functions) 3. List unused functions 4. Delete unused functions (with an IDE like PyCharm) 5. Repeat steps 1 - 4 until all unused functions are deleted a. After deleting some functions, their dependencies may become unused as well 23