Skip to content

Commit 0bd5038

Browse files
committed
pyqt5to6: Catch QVariant.Type.XX and replace with QVariant.XX
QVariant.Type doesn't exist, the values should be referenced directly
1 parent 167e487 commit 0bd5038

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

scripts/pyqt5_to_pyqt6/pyqt5_to_pyqt6.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,22 @@ def visit_attribute(_node: ast.Attribute, _parent):
238238
token_renames[Offset(_node.value.lineno, _node.value.col_offset)] = 'QApplication.instance()'
239239
extra_imports['qgis.PyQt.QtWidgets'].update({'QApplication'})
240240
removed_imports['qgis.PyQt.QtWidgets'].update({'qApp'})
241+
if _node.value.id == 'QVariant' and _node.attr == 'Type':
242+
def _replace_qvariant_type(start_index: int, tokens):
243+
# QVariant.Type.XXX doesn't exist, it should be QVariant.XXX
244+
assert tokens[start_index].src == 'QVariant'
245+
assert tokens[start_index + 1].src == '.'
246+
assert tokens[start_index + 2].src == 'Type'
247+
assert tokens[start_index + 3].src == '.'
248+
249+
tokens[start_index + 2] = tokens[start_index + 2]._replace(src='')
250+
tokens[start_index + 3] = tokens[start_index + 3]._replace(
251+
src='')
252+
253+
custom_updates[Offset(node.lineno, node.col_offset)] = _replace_qvariant_type
241254

242255
tree = ast.parse(contents, filename=filename)
243256
for parent in ast.walk(tree):
244-
245257
for node in ast.iter_child_nodes(parent):
246258
if isinstance(node, ast.ImportFrom):
247259
import_offsets[Offset(node.lineno, node.col_offset)] = (node.module, set(name.name for name in node.names), node.end_lineno, node.end_col_offset)

tests/src/python/test_qgsfieldformatters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def test_representValue(self):
469469
config['TextDisplayMethod'] = QgsCheckBoxFieldFormatter.TextDisplayMethod.ShowTrueFalse
470470
self.assertEqual(field_formatter.representValue(layer, 2, config, None, True), 'true')
471471
self.assertEqual(field_formatter.representValue(layer, 2, config, None, False), 'false')
472-
self.assertEqual(field_formatter.representValue(layer, 2, config, None, QVariant(QVariant.Type.Bool)), 'NULL')
472+
self.assertEqual(field_formatter.representValue(layer, 2, config, None, QVariant(QVariant.Bool)), 'NULL')
473473

474474

475475
class TestQgsFallbackFieldFormatter(QgisTestCase):

0 commit comments

Comments
 (0)