Skip to content

Type annotate Metafunc#6735

Merged
bluetech merged 1 commit intopytest-dev:masterfrom
bluetech:metafunc-annotate
Feb 15, 2020
Merged

Type annotate Metafunc#6735
bluetech merged 1 commit intopytest-dev:masterfrom
bluetech:metafunc-annotate

Conversation

@bluetech
Copy link
Member

Extracted from #6717.

:rtype: List[str]
:return: the list of ids for each argname given
"""
idfn = None
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 rearranged the code here to make it amenable to mypy. I also think it's a bit clearer.

func_name: str,
) -> List[Union[None, str]]:
try:
len(ids)
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 had to refactor a bit to make it amenable to mypy. It's meant to be equivalent.

except TypeError:
raise TypeError("ids must be a callable, sequence or generator")
else:
import itertools
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 moved this import up -- don't see why it should be lazy.

)
else:
if isinstance(indirect, (tuple, list)):
if isinstance(indirect, Sequence):
Copy link
Member Author

Choose a reason for hiding this comment

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

Generalized this a bit to make typing easier. It's also what other functions already check - was inconsistent.

:param indirect: same ``indirect`` parameter of ``parametrize()``.
:raise ValueError: if validation fails
"""
if isinstance(indirect, bool) and indirect is True:
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to rearrange things here a bit.

elif isinstance(val, enum.Enum):
return str(val)
elif hasattr(val, "__name__") and isinstance(val.__name__, str):
elif isinstance(getattr(val, "__name__", None), str):
Copy link
Member Author

Choose a reason for hiding this comment

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

Changed a bit to avoid mypy attr warnings.

def pytest_generate_tests(metafunc: "Metafunc") -> None:
for marker in metafunc.definition.iter_markers(name="parametrize"):
metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)
# TODO: Fix this type-ignore (overlapping kwargs).
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have a plan for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really. There's a workaround of creating kwargs = {**marker.kwargs, '_param_marker': marker} but I think it's worse than the disease.

@bluetech
Copy link
Member Author

Thanks @blueyed, I applied the changes.

@bluetech bluetech merged commit 7b8968f into pytest-dev:master Feb 15, 2020
@bluetech bluetech deleted the metafunc-annotate branch February 28, 2020 12:08
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.

2 participants