Skip to content

Add type annotations to _pytest.config.argparsing and corresponding Config code#6241

Merged
bluetech merged 2 commits intopytest-dev:featuresfrom
bluetech:type-annotations-9
Nov 21, 2019
Merged

Add type annotations to _pytest.config.argparsing and corresponding Config code#6241
bluetech merged 2 commits intopytest-dev:featuresfrom
bluetech:type-annotations-9

Conversation

@bluetech
Copy link
Member

The first commit cleans up some old feature that slightly interfered with the types. The second commit adds annotations to the argument parsing code.

This feature was added in commit
007a77c, but was never used in pytest
itself. A GitHub code search doesn't find any users either (only pytest
repo copies). It seems safe to clean up.
return group

def addoption(self, *opts, **attrs):
def addoption(self, *opts: str, **attrs: Any) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great if attrs could be typed. Would be the same as with Argument.__init__'s attrs then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, TypeScript has this feature, but mypy doesn't AFAIK. The other option is to duplicate, but I wouldn't want to do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could a typevar be used here, to be used in those (3+) places?
But this could also be done later anyway - I've just thought it would be really helpful with completion of those functions.
(You have probably noticed also that documentation in this regard is outdated etc, and it's messy in general, so that would help there.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Could a typevar be used here, to be used in those (3+) places?

Not that I know of...

@bluetech
Copy link
Member Author

Thanks for taking a look @blueyed - updated.

Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

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

Thanks!

@bluetech
Copy link
Member Author

@blueyed I missed a couple of places that can take a py.path.local arg, so I fixed them. Also updated a doc link.

@blueyed
Copy link
Contributor

blueyed commented Nov 21, 2019

I missed a couple of places that can take a py.path.local arg

How does that behave by now? IIRC it's handled as Any still?
From a quick check it means that also a: Any = "foo" is valid there then also.

I think it makes sense therefore to type py.path.local after all?
Could we include a .pyi file in pytest for it?
(but that's for another issue, this PR is good!)

@bluetech
Copy link
Member Author

How does that behave by now? IIRC it's handled as Any still?

It's still Any. But for now I think it might be better to just use it anyway, even if it's unchecked, just for reference.

Could we include a .pyi file in pytest for it?

That would be good, it does show up everywhere in pytest.

I'm not entirely sure we should add it in pytest - what would happen when we finally publish our types and a user encounters py.path.local type? I don't think the pytest one will be used, or should be used. So it might work for local development, but will be better to add to py itself.

@bluetech bluetech merged commit 5820c5c into pytest-dev:features Nov 21, 2019
@bluetech bluetech deleted the type-annotations-9 branch November 21, 2019 11:33
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