Allow getPrimary to return None#16
Conversation
| _arrangeMonitors(arrangement) | ||
|
|
||
|
|
||
| def getMousePos() -> Point: |
There was a problem hiding this comment.
I restored this from https://github.com/Kalmat/PyMonCtl/pull/10/changes#diff-d89140b51ca081cadba27fc89d5e5521978012b254f1c3a593e24a3f1ab67d18L200 , because I realized you may not want to have to duplicate the docstring in 3 places. And it went against the project's established patterns.
| getPrimary() or findMonitor(x, y). | ||
|
|
||
| It can raise ValueError exception in case provided handle is not valid | ||
| or there's no primary monitor with no provided handle. |
There was a problem hiding this comment.
Not sure in which cases something similar could even happen on macOS and Windows.
| def getPrimary() -> Monitor | None: | ||
| """ | ||
| Get primary monitor instance. This is equivalent to invoking ''Monitor()'', with empty input params. | ||
| Get primary monitor instance. | ||
|
|
||
| This is equivalent to invoking `Monitor()`, with empty input params. | ||
| With a fallback to None in case of `ValueError`. | ||
| Which usually means lack of primary (or any) monitor. | ||
|
|
||
| :return: Monitor instance or None | ||
| """ | ||
| return _getPrimary() | ||
| try: | ||
| return Monitor() | ||
| except ValueError: | ||
| return None |
There was a problem hiding this comment.
Decision to be taken, as this is a change of public API. Docstring said it could be None, but static type, and runtime reality, could never be None.
imo this is preferable as the possible failure mode is less "hidden" (thrown exception), whilst type checkers will force you to handle the None case.
1f9981d to
a4e55fa
Compare
Allow
getPrimaryto returnNoneif the underlyingMonitor()call throws aValueError(no primary monitor)Note that the docstring was even mentioning a possible
Nonereturn type !