Skip to content

Allow getPrimary to return None#16

Open
Avasam wants to merge 1 commit into
masterfrom
getPrimary-to-return-None
Open

Allow getPrimary to return None#16
Avasam wants to merge 1 commit into
masterfrom
getPrimary-to-return-None

Conversation

@Avasam

@Avasam Avasam commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Allow getPrimary to return None if the underlying Monitor() call throws a ValueError (no primary monitor)
Note that the docstring was even mentioning a possible None return type !

@Avasam Avasam requested a review from Kalmat June 24, 2026 23:29
Comment thread src/pymonctl/_main.py
_arrangeMonitors(arrangement)


def getMousePos() -> Point:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure in which cases something similar could even happen on macOS and Windows.

Comment thread src/pymonctl/_main.py
Comment on lines +98 to +111
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

@Avasam Avasam Jun 24, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Avasam Avasam force-pushed the getPrimary-to-return-None branch from 1f9981d to a4e55fa Compare June 24, 2026 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant