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

Implement ADR 0002 (remove special handling of Optional and Union) #158

Merged
merged 5 commits into from
Apr 17, 2024

Conversation

SimonHeybrock
Copy link
Member

@SimonHeybrock SimonHeybrock commented Apr 15, 2024

While Pipeline will be re-implemented, I nevertheless made this change, since it allows for updating units tests with new expected behavior, which will make the re-implementation easier/smaller (will pass the same unit tests), decoupling the behavior change from the re-implementation.

@SimonHeybrock SimonHeybrock changed the base branch from main to drop-py39 April 15, 2024 10:04
@@ -149,7 +149,7 @@ def _find_nodes_in_paths(graph: Graph, end: Key) -> List[Key]:


def _is_multiple_keys(
keys: type | Iterable[type] | Item[T],
keys: type | Iterable[type] | Item[T] | object,
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this equivalent to keys: Any now? Why did you add object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I could not get it to work otherwise. I thought listing the others still serves as an expression of intent?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe. So did it not work with Any? From what I understand that is preferred over object.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I mean I failed to find another more concrete type hint that works. Any would work.

@@ -793,7 +752,13 @@ def compute(self, tp: Iterable[Type[T]], **kwargs: Any) -> Dict[Type[T], T]:
def compute(self, tp: Item[T], **kwargs: Any) -> T:
...

def compute(self, tp: type | Iterable[type] | Item[T], **kwargs: Any) -> Any:
@overload
def compute(self, tp: object, **kwargs: Any) -> Any:
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above, this is equivalent to

Suggested change
def compute(self, tp: object, **kwargs: Any) -> Any:
def compute(self, tp: Any, **kwargs: Any) -> Any:

if I'm not mistaken.
Does mypy use the specialised overloads where possible or does it always fall back to Any?



def test_union_with_none_can_be_used_instead_of_Optional() -> None:
def test_Union_and_optional_are_distinct() -> None:
Copy link
Member

Choose a reason for hiding this comment

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

I don't think Union[T, None] and Optional[T] should be treated differently.
But this test doesn't actually seem to have anything to do with Optional. It just tests different argument orders.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think Union[T, None] and Optional[T] should be treated differently.

Then we would need to add special-case handling.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, now I think I understand what you want to test here. But there are no optionals in the test, only unions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, fixed I think!

Base automatically changed from drop-py39 to main April 15, 2024 11:42
@MridulS
Copy link
Member

MridulS commented Apr 15, 2024

We should probably change

params: Optional[Dict[Type[Any], Any]] = None,
to Optional[Dict[Any, Any]] maybe with a comment about the weird behaviour of what is a type vs type hint?

@SimonHeybrock
Copy link
Member Author

I am thinking now that we should remove the object hints, and disable mypy warnings on a case-by-case basis where Optional is used. It seems either we need a fundamentally new idea how to handle Optional, or just avoid it entirely?

@SimonHeybrock SimonHeybrock merged commit e98f4a4 into main Apr 17, 2024
5 checks passed
@SimonHeybrock SimonHeybrock deleted the remove-optional-union branch April 17, 2024 09:46
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.

3 participants