remove_debug=True incorrectly removes if/elif/else blocks comparing with 'is True'
Description
When using remove_debug=True, the minifier incorrectly removes entire if/elif/else blocks that use is True comparisons, even when these are functional code and not debug statements.
Reproduction
import python_minifier
source = '''
def check_is_internet_working(c):
url, url_hostname = get_url_and_url_hostname(c)
if is_internet_working_socket_test(c, url_hostname) is True:
c.is_internet_connected = True
elif is_internet_working_urllib_open(c, url) is True:
c.is_internet_connected = True
else:
c.is_internet_connected = False
return c.is_internet_connected
'''
# With remove_debug=True - BROKEN
result = python_minifier.minify(source, remove_debug=True)
print(result)
Expected Output
The if/elif/else block should be preserved (perhaps with is True simplified):
def check_is_internet_working(c):
A,B=get_url_and_url_hostname(c)
if is_internet_working_socket_test(c,B):c.is_internet_connected=True
elif is_internet_working_urllib_open(c,A):c.is_internet_connected=True
else:c.is_internet_connected=False
return c.is_internet_connected
Actual Output
The entire if/elif/else block is removed:
def check_is_internet_working(c):A,B=get_url_and_url_hostname(c);return c.is_internet_connected
Impact
This is a silent, breaking bug - the minified code passes syntax validation but produces completely different runtime behavior. In our case, this caused production IoT gateways to fail internet connectivity checks because the function that sets c.is_internet_connected = True was completely removed.
Environment
- python-minifier version: 3.1.0
- Python version: 3.12.3
Analysis
The remove_debug option appears to be treating func() is True patterns as debug assertions (similar to assert statements or if __debug__: blocks). However, is True comparisons are valid, functional code patterns, especially when checking for explicit True vs truthy values.
The minifier should either:
- Not remove
is Truecomparisons underremove_debug - Document this behavior prominently as a breaking change risk
- Provide a separate option for this specific transformation
Workaround
Set remove_debug=False when minifying code that uses is True comparisons in conditional logic.
Minimal Reproducible Example
import python_minifier
source = '''
def check_internet():
if is_connected() is True:
result = 'online'
else:
result = 'offline'
return result
'''
print('With remove_debug=True:')
print(python_minifier.minify(source, remove_debug=True))
print('With remove_debug=False:')
print(python_minifier.minify(source, remove_debug=False))
Output:
With remove_debug=True:
def check_internet():return result
With remove_debug=False:
def check_internet():
if is_connected()is True:A='online'
else:A='offline'
return A
With remove_debug=True, the entire if/else block is removed, and the function returns an undefined result variable.
Hello @mccarthysean, thanks for creating an issue. This should be resolved in v3.1.1