Force type hints#233
Conversation
eb9b5c2 to
b7e82ba
Compare
| @@ -1,7 +1,11 @@ | |||
| [flake8] | |||
There was a problem hiding this comment.
This whole file is very cargo-culted. Can we clean it up while we're in here?
| object.__setattr__(self, "_compare_key", self._make_compare_key()) | ||
|
|
||
| def _make_compare_key(self): | ||
| def _make_compare_key( |
There was a problem hiding this comment.
Wow, this is a complicated type. Is there any way to clean this up with NewType, a refactor with an object, or similar?
There was a problem hiding this comment.
I was happy, that PyCharm was doing this for me 😆 IMO refactoring should be done in another PR, because the purpose of this PR is to enforce type hints in the future.
There was a problem hiding this comment.
That is a brutal type signature :)
There was a problem hiding this comment.
I've tried to understand the code and IMO the signature can be written more easily. See code change.
danieleades
left a comment
There was a problem hiding this comment.
mypy itself can be configured to return errors on missing annotations, i believe. is the 'flake8' plugin somehow preferable?
if TYPE_CHECKINGpattern doesn't appear to be required throughout- inconsistent use of
dict/Dict
|
|
||
|
|
||
| def pytest_addoption(parser): | ||
| if TYPE_CHECKING: |
There was a problem hiding this comment.
you shouldn't need the if TYPE_CHECKING pattern here- that's really only for preventing circular imports. You can just import these directly, and use them below without quoting the types
There was a problem hiding this comment.
That's a convention I'm used to. If you need a module just for type hints, import it only in the if TYPE_CHECKING block.
It's easier to teach to other people ("Why are you sometime put import in that block and sometimes not?") and the overall chance that you have a circular import if you need this import just for type hint is quite big.
TL;DR: I prefer to leave it as it is.
There was a problem hiding this comment.
fair enough. there's some interesting background on it here - larryhastings/co_annotations#1
in general, it seems that if TYPE_CHECKING is used iff
- you would otherwise have a circular import
- the import is 'expensive'
the Benevolent Dictator for Life seems to regard it is a painfully necessary hack.
that said, it's also clear from the thread that it's far from settled. And using it everywhere instead of the small number of cases where you need it (do you definitely need it anywhere in this codebase?) is at least consistent.
|
|
||
|
|
||
| def pytest_configure(config): | ||
| def pytest_configure(config: "Config") -> None: |
There was a problem hiding this comment.
you can use Config unquoted if you remove the if TYPE_CHECKING pattern above
|
|
||
| @pytest.fixture | ||
| def base_object(): | ||
| def base_object() -> dict: |
There was a problem hiding this comment.
this syntax is python 3.9+. less than that, the correct return type would be Dict, rather than dict.
I guess we can get away with it, since mypy is being run with a recent interpreter, and the type is ignored during other testing, but whichever way we go, it should be consistent throughout the codebase
There was a problem hiding this comment.
while we're at it, these dict annotations are equivalent to Dict[Any, Any]- we should be able to do better than that
There was a problem hiding this comment.
As you find out dict is valid as well, but don't let us define how keys and values look like in python<3.9. I agree it's in inconsistent use if we use Dict in other places, so I changed it.
while we're at it, these dict annotations are equivalent to Dict[Any, Any]- we should be able to do better than that
The only way to do it better here is to use Dict[str, Any] (which doesn't add much benefit) or use TypedDict. But this is only available for python >=3.8 and would require to add typing-extensions as dependency for python <3.8. Not sure if we want to introduce another dependency.
|
|
||
|
|
||
| def test_builder_find_excluded_files(mocker): | ||
| if TYPE_CHECKING: |
There was a problem hiding this comment.
is there a circular import if you do not guard the import of MockerFixture?
17e23a7 to
36ac1ce
Compare
| def parse( | ||
| cls, value: str, version_class: Optional[Type["PEP440Version"]] = None | ||
| ) -> "PEP440Version": |
There was a problem hiding this comment.
if you remove the if TYPE_CHECKING guard, this can be written as-
| def parse( | |
| cls, value: str, version_class: Optional[Type["PEP440Version"]] = None | |
| ) -> "PEP440Version": | |
| def parse( | |
| cls, value: str, version_class: Optional[PEP440Version] = None | |
| ) -> PEP440Version: |
a8847b5 to
de16fac
Compare
|
might be a good idea to merge #199 before this is merged, in order to catch any type errors that could be interested by this PR |
|
re: Looks like a useful set of lints we could apply here. |
de16fac to
eb6c089
Compare
eb6c089 to
38bbcff
Compare
This PR add missing type hints to both package code and test code. Furthermore
flake-annotationsis added as a flake8 plugin to enforce type hints in the future.