Skip to content

Commit 42afbc8

Browse files
authored
PR: Symmetrize QDateTime.toPython and toPyDateTime, etc. (#421)
2 parents bc7a3c7 + b3e831d commit 42afbc8

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

qtpy/QtCore.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
from PyQt5.QtCore import pyqtProperty as Property
2020
from PyQt5.QtCore import QT_VERSION_STR as __version__
2121

22-
# For issue #153 and updated for issue #305
23-
from PyQt5.QtCore import QDate, QDateTime, QTime
24-
QDate.toPython = lambda self, *args, **kwargs: self.toPyDate(*args, **kwargs)
25-
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(*args, **kwargs)
26-
QTime.toPython = lambda self, *args, **kwargs: self.toPyTime(*args, **kwargs)
27-
2822
# Those are imported from `import *`
2923
del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, QT_VERSION_STR
3024

@@ -37,12 +31,6 @@
3731
from PyQt6.QtCore import pyqtProperty as Property
3832
from PyQt6.QtCore import QT_VERSION_STR as __version__
3933

40-
# For issue #153 and updated for issue #305
41-
from PyQt6.QtCore import QDate, QDateTime, QTime
42-
QDate.toPython = lambda self, *args, **kwargs: self.toPyDate(*args, **kwargs)
43-
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(*args, **kwargs)
44-
QTime.toPython = lambda self, *args, **kwargs: self.toPyTime(*args, **kwargs)
45-
4634
# For issue #311
4735
# Seems like there is an error with sip. Without first
4836
# trying to import `PyQt6.QtGui.Qt`, some functions like
@@ -110,6 +98,16 @@
11098
QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
11199
QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
112100

101+
# For issue #153 and updated for issue #305
102+
if PYQT5 or PYQT6:
103+
QDate.toPython = lambda self, *args, **kwargs: self.toPyDate(*args, **kwargs)
104+
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(*args, **kwargs)
105+
QTime.toPython = lambda self, *args, **kwargs: self.toPyTime(*args, **kwargs)
106+
if PYSIDE2 or PYSIDE6:
107+
QDate.toPyDate = lambda self, *args, **kwargs: self.toPython(*args, **kwargs)
108+
QDateTime.toPyDateTime = lambda self, *args, **kwargs: self.toPython(*args, **kwargs)
109+
QTime.toPyTime = lambda self, *args, **kwargs: self.toPython(*args, **kwargs)
110+
113111
# Mirror https://github.com/spyder-ide/qtpy/pull/393
114112
if PYQT5 or PYSIDE2:
115113
QLibraryInfo.path = QLibraryInfo.location

qtpy/tests/test_qtcore.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test QtCore."""
22

3-
from datetime import date, datetime, time
43
import sys
4+
from datetime import date, datetime, time
55

66
import pytest
77

@@ -16,34 +16,41 @@
1616
)
1717
from qtpy.tests.utils import not_using_conda
1818

19+
_now = datetime.now()
20+
# Make integer milliseconds; `floor` here, don't `round`!
21+
NOW = _now.replace(microsecond=(_now.microsecond // 1000 * 1000))
22+
1923

2024
def test_qtmsghandler():
2125
"""Test qtpy.QtMsgHandler"""
2226
assert QtCore.qInstallMessageHandler is not None
2327

2428

25-
def test_qdatetime_toPython():
26-
"""Test QDateTime.toPython"""
27-
q_date = QtCore.QDateTime.currentDateTime()
28-
assert QtCore.QDateTime.toPython is not None
29-
py_date = q_date.toPython()
30-
assert isinstance(py_date, datetime)
29+
@pytest.mark.parametrize('method', ['toPython', 'toPyDateTime'])
30+
def test_QDateTime_toPython_and_toPyDateTime(method):
31+
"""Test `QDateTime.toPython` and `QDateTime.toPyDateTime`"""
32+
q_datetime = QtCore.QDateTime(NOW)
33+
py_datetime = getattr(q_datetime, method)()
34+
assert isinstance(py_datetime, datetime)
35+
assert py_datetime == NOW
3136

3237

33-
def test_qdate_toPython():
34-
"""Test QDate.toPython"""
35-
q_date = QtCore.QDate.currentDate()
36-
assert QtCore.QDate.toPython is not None
37-
py_date = q_date.toPython()
38+
@pytest.mark.parametrize('method', ['toPython', 'toPyDate'])
39+
def test_QDate_toPython_and_toPyDate(method):
40+
"""Test `QDate.toPython` and `QDate.toPyDate`"""
41+
q_date = QtCore.QDateTime(NOW).date()
42+
py_date = getattr(q_date, method)()
3843
assert isinstance(py_date, date)
44+
assert py_date == NOW.date()
3945

4046

41-
def test_qtime_toPython():
42-
"""Test QTime.toPython"""
43-
q_time = QtCore.QTime.currentTime()
44-
assert QtCore.QTime.toPython is not None
45-
py_time = q_time.toPython()
47+
@pytest.mark.parametrize('method', ['toPython', 'toPyTime'])
48+
def test_QTime_toPython_and_toPyTime(method):
49+
"""Test `QTime.toPython` and `QTime.toPyTime`"""
50+
q_time = QtCore.QDateTime(NOW).time()
51+
py_time = getattr(q_time, method)()
4652
assert isinstance(py_time, time)
53+
assert py_time == NOW.time()
4754

4855

4956
@pytest.mark.skipif(

0 commit comments

Comments
 (0)