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

Fill missing type hints #241

Closed
wants to merge 20 commits into from
Closed

Fill missing type hints #241

wants to merge 20 commits into from

Conversation

eggplants
Copy link

@eggplants eggplants commented Mar 2, 2022

Closes: #240

typeguard.typechecking is too complex to annotate correctly. So please tell me your advice, or annotate it instead of me.

$ mypy src
src/typeguard/__init__.py:1133: error: Function is missing a return type
annotation  [no-untyped-def]
    def typechecked(func=None, *, always=False, _localns: Optional[Dict[st...
    ^
src/typeguard/__init__.py:1133: error: Function is missing a type annotation
for one or more arguments  [no-untyped-def]
    def typechecked(func=None, *, always=False, _localns: Optional[Dict[st...
    ^
src/typeguard/__init__.py:1164: error: No overload variant of "typechecked"
matches argument types "Union[FunctionType, MethodType, Type[Any]]", "Any",
"Dict[str, Any]"  [call-overload]
                            typechecked(attr, always=always, _localns=func...
                            ^
src/typeguard/__init__.py:1164: note: Possible overload variants:
src/typeguard/__init__.py:1164: note:     def typechecked(*, always: bool = ...) -> Callable[[T_CallableOrType], T_CallableOrType]
src/typeguard/__init__.py:1164: note:     def [T_CallableOrType] typechecked(func: T_CallableOrType, *, always: bool = ...) -> T_CallableOrType
src/typeguard/__init__.py:1168: error: No overload variant of "typechecked"
matches argument types "Callable[..., Any]", "Any", "Dict[str, Any]" 
[call-overload]
                        wrapped = typechecked(attr.__func__, always=always...
                                  ^
src/typeguard/__init__.py:1168: note: Possible overload variants:
src/typeguard/__init__.py:1168: note:     def typechecked(*, always: bool = ...) -> Callable[[T_CallableOrType], T_CallableOrType]
src/typeguard/__init__.py:1168: note:     def [T_CallableOrType] typechecked(func: T_CallableOrType, *, always: bool = ...) -> T_CallableOrType
src/typeguard/__init__.py:1174: error: Argument 3 to "getattr" has incompatible
type "Tuple[]"; expected "bool"  [arg-type]
    ...ty_func is not None and getattr(property_func, "__annotations__", ()):
                                                                         ^
src/typeguard/__init__.py:1175: error: No overload variant of "typechecked"
matches argument types "Any", "Any", "Dict[str, Any]"  [call-overload]
                            kwargs[name] = typechecked(
                                           ^
src/typeguard/__init__.py:1175: note: Possible overload variants:
src/typeguard/__init__.py:1175: note:     def typechecked(*, always: bool = ...) -> Callable[[T_CallableOrType], T_CallableOrType]
src/typeguard/__init__.py:1175: note:     def [T_CallableOrType] typechecked(func: T_CallableOrType, *, always: bool = ...) -> T_CallableOrType
src/typeguard/__init__.py:1179: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], Any]]"  [arg-type]
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1179: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any, Any], None]]"  [arg-type]
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1179: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], None]]"  [arg-type]
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1219: error: Returning Any from function declared to
return
"Union[TypeCheckedGenerator, TypeCheckedAsyncGenerator, GeneratorType[Any, Any, Any]]"
 [no-any-return]
            return retval
            ^
Found 10 errors in 1 file (checked 3 source files)

@eggplants
Copy link
Author

eggplants commented Mar 2, 2022

@agronholm .pre-commit-config.yaml is not found.

@agronholm
Copy link
Owner

I haven't added the pre-commit configuration to this project yet.

@agronholm
Copy link
Owner

Why have you added all sorts of unrelated configuration?

@eggplants
Copy link
Author

Why have you added all sorts of unrelated configuration?

Sorry, removed.

@agronholm
Copy link
Owner

Closes: #240

typeguard.typechecking is too complex to annotate correctly. So please tell me your advice, or annotate it instead of me.

$ mypy src
src/typeguard/__init__.py:1133: error: Function is missing a return type
annotation  [no-untyped-def]
    def typechecked(func=None, *, always=False, _localns: Optional[Dict[st...
    ^
src/typeguard/__init__.py:1133: error: Function is missing a type annotation
for one or more arguments  [no-untyped-def]
    def typechecked(func=None, *, always=False, _localns: Optional[Dict[st...
    ^
src/typeguard/__init__.py:1164: error: No overload variant of "typechecked"
matches argument types "Union[FunctionType, MethodType, Type[Any]]", "Any",
"Dict[str, Any]"  [call-overload]
                            typechecked(attr, always=always, _localns=func...
                            ^
src/typeguard/__init__.py:1164: note: Possible overload variants:
src/typeguard/__init__.py:1164: note:     def typechecked(*, always: bool = ...) -> Callable[[T_CallableOrType], T_CallableOrType]
src/typeguard/__init__.py:1164: note:     def [T_CallableOrType] typechecked(func: T_CallableOrType, *, always: bool = ...) -> T_CallableOrType
src/typeguard/__init__.py:1168: error: No overload variant of "typechecked"
matches argument types "Callable[..., Any]", "Any", "Dict[str, Any]" 
[call-overload]
                        wrapped = typechecked(attr.__func__, always=always...
                                  ^
src/typeguard/__init__.py:1168: note: Possible overload variants:
src/typeguard/__init__.py:1168: note:     def typechecked(*, always: bool = ...) -> Callable[[T_CallableOrType], T_CallableOrType]
src/typeguard/__init__.py:1168: note:     def [T_CallableOrType] typechecked(func: T_CallableOrType, *, always: bool = ...) -> T_CallableOrType
src/typeguard/__init__.py:1174: error: Argument 3 to "getattr" has incompatible
type "Tuple[]"; expected "bool"  [arg-type]
    ...ty_func is not None and getattr(property_func, "__annotations__", ()):
                                                                         ^
src/typeguard/__init__.py:1175: error: No overload variant of "typechecked"
matches argument types "Any", "Any", "Dict[str, Any]"  [call-overload]
                            kwargs[name] = typechecked(
                                           ^
src/typeguard/__init__.py:1175: note: Possible overload variants:
src/typeguard/__init__.py:1175: note:     def typechecked(*, always: bool = ...) -> Callable[[T_CallableOrType], T_CallableOrType]
src/typeguard/__init__.py:1175: note:     def [T_CallableOrType] typechecked(func: T_CallableOrType, *, always: bool = ...) -> T_CallableOrType
src/typeguard/__init__.py:1179: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], Any]]"  [arg-type]
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1179: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any, Any], None]]"  [arg-type]
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1179: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], None]]"  [arg-type]
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1219: error: Returning Any from function declared to
return
"Union[TypeCheckedGenerator, TypeCheckedAsyncGenerator, GeneratorType[Any, Any, Any]]"
 [no-any-return]
            return retval
            ^
Found 10 errors in 1 file (checked 3 source files)

You should broaden the annotations, possibly using Any where the proper type cannot be determined.

@agronholm
Copy link
Owner

There are code styling changes not related to annotations. Please undo those changes.

The GitHub Actions workflow should have a step to run mypy. You should add that.

@eggplants
Copy link
Author

@agronholm done.

@eggplants
Copy link
Author

$ git rev-parse HEAD # now
e84ca55ff3d2743a0924e08ad088f7470f3a4745
$ mypy tests/mypy/test_type_annotations.py src
src/typeguard/__init__.py:1030: error: Argument 3 to "getattr" has incompatible
type "Tuple[]"; expected "bool"
    ...ty_func is not None and getattr(property_func, '__annotations__', ()):
                                                                         ^
src/typeguard/__init__.py:1033: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], Any]]"
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1033: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any, Any], None]]"
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1033: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], None]]"
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1057: error: "None" not callable
            retval = func(*args, **kwargs)
                     ^
src/typeguard/__init__.py:1073: error: Returning Any from function declared to
return
"Union[TypeCheckedGenerator, TypeCheckedAsyncGenerator, GeneratorType[Any, Any, Any]]"
            return retval
            ^
src/typeguard/__init__.py:1078: error: "None" not callable
            retval = await func(*args, **kwargs)
                           ^
Found 7 errors in 1 file (checked 4 source files)

@agronholm
Copy link
Owner

This line runs mypy; it should be modified to check the entire codebase.

@eggplants
Copy link
Author

eggplants commented Mar 2, 2022

This line runs mypy; it should be modified to check the entire codebase.

Please check: e84ca55

@agronholm
Copy link
Owner

Those mypy errors look nonsensical. I wonder if pyright gives similar errors.

error: Argument 3 to "getattr" has incompatible type "Tuple[]"; expected "bool"
@agronholm
Copy link
Owner

This line runs mypy; it should be modified to check the entire codebase.

Please check: e84ca55

Ah, quite right.

src/typeguard/__init__.py:1033: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], Any]]"
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1033: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any, Any], None]]"
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1033: error: Argument 1 to "property" has
incompatible type "**Dict[str, Optional[str]]"; expected
"Optional[Callable[[Any], None]]"
                    setattr(func, key, attr.__class__(**kwargs))
                                                        ^
src/typeguard/__init__.py:1057: error: "None" not callable
            retval = func(*args, **kwargs)
                     ^
src/typeguard/__init__.py:1073: error: Returning Any from function declared to
return
"Union[TypeCheckedGenerator, TypeCheckedAsyncGenerator, GeneratorType[Any, Any, Any]]"
            return retval
            ^
src/typeguard/__init__.py:1078: error: "None" not callable
            retval = await func(*args, **kwargs)
@eggplants
Copy link
Author

eggplants commented Mar 2, 2022

$ git rev-parse HEAD # now
a9c2fb9c91303e3d75bb121aeba2ae1655e07b99
$ mypy tests/mypy/test_type_annotations.py src
Success: no issues found in 4 source files
$ pytest
============================= test session starts ==============================
platform darwin -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/eggplants/prog/typeguard, configfile: pyproject.toml, testpaths: tests
plugins: subtests-0.5.0, monkeytype-1.1.0, mock-3.7.0, typeguard-2.13.3, cov-3.0.0
collected 243 items

tests/test_importhook.py .................                               [  6%]
tests/test_typeguard.py ................................................ [ 26%]
........................................................................ [ 56%]
........................................................................ [ 86%]
................                                                         [ 92%]
tests/test_typeguard_py36.py ................                            [ 99%]
tests/mypy/test_type_annotations.py ..                                   [100%]

=============================== warnings summary ===============================
tests/test_typeguard_py36.py::TestTypeChecker::test_callable
  /Users/eggplants/.pyenv/versions/3.9.9/lib/python3.9/site-packages/typeguard/__init__.py:1027: UserWarning: no code associated -- not typechecking test_typeguard_py36.<test_typeguard_py36.TestTypeChecker.test_callable.<locals>.command object at 0x111d40130>
    warn('no code associated -- not typechecking {}'.format(function_name(func)))

-- Docs: https://docs.pytest.org/en/stable/warnings.html
======================== 243 passed, 1 warning in 1.20s ========================

$ grep -r '# type: ignore' src/typeguard/*.py | wc -l
20

20 suppressed errors exist...

src/typeguard/__init__.py:1135:30: E126 continuation line over-indented for hanging indent
@agronholm
Copy link
Owner

I'm sorry I didn't think of this before (I was travelling), but you probably should've based this on the 3.0 branch. I have no intention of updating the 2.x series anymore.

@eggplants eggplants closed this Mar 2, 2022
@eggplants
Copy link
Author

I have not noticed branch 3.0 until now. I'll try to annotate it later.

@eggplants
Copy link
Author

I'm sorry I didn't think of this before (I was travelling), but you probably should've based this on the 3.0 branch. I have no intention of updating the 2.x series anymore.

Please document in either README or Documentation in your free time.

@agronholm
Copy link
Owner

I expected to release 3.0 much sooner but got sidetracked by other work, leading to this awkward situation.

@eggplants
Copy link
Author

eggplants commented Mar 2, 2022

To avoid confusion for others, till 3.0 is published, I recommend either:

  • add a single sentence of caution to the README
  • pin the Issue that describes this information
  • set the Default branch to 3.0
  • ...

Otherwise, more people's time is bound to be wasted.

@eggplants eggplants deleted the add_type_hint branch March 3, 2022 00:36
@eggplants eggplants mentioned this pull request Mar 26, 2022
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.

Follow mypy --strict
2 participants