-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
# Contains Python 3.8 syntax | ||
# flake does not like it when running on older versions of Python | ||
# flake8: noqa | ||
# With mypy, there does not seem to be a way to prevent an error when running in Python < 3.8. | ||
# For this reason, I am including the code as a string (the horror). | ||
# Once we upgrade to mypy 0.812, we should be able to use --exclude and eliminate this hack. | ||
from typing import Any | ||
|
||
code = """ | ||
class PosOnlyArgsClass: | ||
def __init__(self, a: Any, b: Any, /, **kwargs: Any) -> None: | ||
assert isinstance(kwargs, dict) | ||
self.a = a | ||
self.b = b | ||
self.kwargs = kwargs | ||
def __repr__(self) -> str: | ||
return f"{self.a=},{self.b},{self.kwargs=}" | ||
def __eq__(self, other: Any) -> Any: | ||
if isinstance(other, PosOnlyArgsClass): | ||
return ( | ||
self.a == other.a and self.b == other.b and self.kwargs == other.kwargs | ||
) | ||
else: | ||
return NotImplemented | ||
""" | ||
|
||
|
||
# Dummy class to keep mypy happy | ||
class PosOnlyArgsClass: | ||
def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
... | ||
|
||
|
||
exec(code) # nosec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
import sys | ||
from typing import Any | ||
|
||
from pytest import mark, param, skip | ||
|
||
from hydra.utils import instantiate | ||
|
||
if sys.version_info < (3, 8): | ||
skip( | ||
msg="Positional-only syntax is only supported in Python 3.8 or newer", | ||
allow_module_level=True, | ||
) | ||
|
||
|
||
from .positional_only import PosOnlyArgsClass | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "args", "expected"), | ||
[ | ||
param( | ||
{ | ||
"_target_": "tests.instantiate.positional_only.PosOnlyArgsClass", | ||
"_args_": [1, 2], | ||
}, | ||
[], | ||
PosOnlyArgsClass(1, 2), | ||
id="pos_only_in_config", | ||
), | ||
param( | ||
{ | ||
"_target_": "tests.instantiate.positional_only.PosOnlyArgsClass", | ||
}, | ||
[1, 2], | ||
PosOnlyArgsClass(1, 2), | ||
id="pos_only_in_override", | ||
), | ||
param( | ||
{ | ||
"_target_": "tests.instantiate.positional_only.PosOnlyArgsClass", | ||
"_args_": [1, 2], | ||
}, | ||
[3, 4], | ||
PosOnlyArgsClass(3, 4), | ||
id="pos_only_in_both", | ||
), | ||
], | ||
) | ||
def test_positional_only_arguments(cfg: Any, args: Any, expected: Any) -> None: | ||
assert instantiate(cfg, *args) == expected |