Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Region.EMPTY instead of None #624

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

wmanley
Copy link
Contributor

@wmanley wmanley commented Sep 17, 2019

This may make it easier to use functions that return a Region that might
be None because in many cases they may not need to check region is None
before performing operations on that region. It also makes it easier to
implement functions with take a default Region because we can follow the
standard region is not None to mean "did the caller pass a region
argument". For an example of this see Grid.

I don't think this should go in the next release. It would be better to let it bake for a while.

TODO:

  • Should Region.NONE == Region.NONE? Currently Region.NONE == Region.NONE, but Region.NONE != Region(float('nan'), float('nan'), float('nan'), float('nan')) because NAN != NAN and NAN != float('nan') but (NAN,) == (NAN,) and (NAN,) != (float('nan'))
  • Should Region.NONE.width == 0 or nan?, equally for height
  • Is NONE a good name? Perhaps EMPTY would be better?
  • Codify all these weird behaviours in tests so we'll know if we break them
  • Validate that we've converted all the uses of None as Region in our codebase.
  • Release note

drothlis added a commit that referenced this pull request Jun 12, 2022
`None` means an empty region, for example the result of
`Region.intersect` if the input regions don't overlap.

We can handle `None` on either side of the operator, by implementing
the reflected operators (`__radd__` and `__rsub__`). See
https://docs.python.org/3/reference/datamodel.html#object.__radd__

We still can't support `None + None` but that's a limitation I can live
with for now. Fixing this properly will require adding a new singleton
to represent empty regions, changing `Region.intersect` to return that
instead of `None`, and updating the implementation of all our other
region-handling code. There's a (out of date) pull request for that:
#624
@drothlis
Copy link
Contributor

Should Region.NONE == Region.NONE? Currently Region.NONE == Region.NONE, but Region.NONE != Region(float('nan'), float('nan'), float('nan'), float('nan')) because NAN != NAN and NAN != float('nan') but (NAN,) == (NAN,) and (NAN,) != (float('nan'))

Yes, Region.NONE == Region.NONE.

Should Region.NONE.width == 0 or nan?, equally for height

0, I think.

Is NONE a good name? Perhaps EMPTY would be better?

EMPTY, definitely better.

@drothlis
Copy link
Contributor

but (NAN,) == (NAN,)

Interesting. That's still true as of Python 3.10.5.

@drothlis drothlis changed the title Replace treating None as Region with Region.NONE Region.EMPTY instead of None Jun 12, 2022
@wmanley wmanley force-pushed the Region.NONE branch 2 times, most recently from 633f783 to 391a83f Compare June 13, 2022 13:41
I think this is a bit neater.
This may make it easier to use functions that return a `Region` that might
be `None` because in many cases they may not need to check `region is None`
before performing operations on that region.  It also makes it easier to
implement functions with take a default `Region` because we can follow the
standard `region is not None` to mean "did the caller pass a region
argument".  For an example of this see `Grid`.
@wmanley
Copy link
Contributor Author

wmanley commented Jun 13, 2022

Rebased on master to resolve conflicts.

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.

None yet

2 participants