Skip to content

Use type hint for method declaration#8248

Merged
niboshi merged 18 commits intochainer:masterfrom
himkt:use-type-hint
Oct 11, 2019
Merged

Use type hint for method declaration#8248
niboshi merged 18 commits intochainer:masterfrom
himkt:use-type-hint

Conversation

@himkt
Copy link
Copy Markdown
Member

@himkt himkt commented Oct 4, 2019

Close #8240.

[edited: 2019/10/08] I revised module list by running following command.
grep -v "=.\+# type: " **/*.py| grep "# type: "

This intended to exclude variable annotations. (#8248 (comment))

  • chainer/backend.py
  • chainer/backends/_chainerx.py
  • chainer/backends/cuda.py
  • chainer/backends/intel64.py (no method affected)
  • chainer/device_resident.py
  • chainer/function.py
  • chainer/initializer.py
  • chainer/initializers/init.py
  • chainer/iterators/multiprocess_iterator.py (no method affected)
  • chainer/link.py
  • chainer/link_hook.py
  • chainer/links/connection/linear.py
  • chainer/testing/parameterized.py
  • chainer/types.py
  • chainer/variable.py
  • chainermn/communicators/communicator_base.py
  • docs/source/_napoleon_patch.py

grep result

Details
chainer/backend.py:    # type: (types.DeviceSpec) -> Device
chainer/backends/_chainerx.py:        # type: (chainerx.Device) -> None
chainer/backends/cuda.py:    from cupy import ndarray  # type: ignore # NOQA
chainer/backends/cuda.py:    from cupy.cuda import Device  # type: ignore # NOQA
chainer/backends/cuda.py:    from cupy.cuda import Event  # type: ignore # NOQA
chainer/backends/cuda.py:    from cupy.cuda import Stream  # type: ignore # NOQA
chainer/backends/cuda.py:    class ndarray(object):  # type: ignore # for type testing
chainer/backends/cuda.py:            # type: () -> types.Shape
chainer/backends/cuda.py:            # type: () -> 'Device'
chainer/backends/cuda.py:            # type: (tp.Optional['Stream']) -> numpy.ndarray
chainer/backends/cuda.py:            # type: (numpy.ndarray, tp.Optional['Stream']) -> None
chainer/backends/cuda.py:    class Device(object):  # type: ignore # for type testing
chainer/backends/cuda.py:            # type: (tp.Optional[int]) -> None
chainer/backends/cuda.py:            # type: () -> 'Device'
chainer/backends/cuda.py:            # type: (*tp.Any) -> None
chainer/backends/cuda.py:    class Event(object):  # type: ignore # for type testing
chainer/backends/cuda.py:    class Stream(object):  # type: ignore # for type testing
chainer/backends/cuda.py:    # type: (tp.Optional[int]) -> Device
chainer/backends/cuda.py:    # type: (*ndarray) -> Device
chainer/backends/cuda.py:    # type: (tp.Optional[types.CudaDeviceSpec]) -> Device
chainer/backends/intel64.py:    from ideep4py import mdarray  # type: ignore # NOQA
chainer/backends/intel64.py:    class mdarray(object):  # type: ignore
chainer/device_resident.py:        # type: () -> types.Xp
chainer/device_resident.py:        # type: () -> 'DeviceResident'
chainer/device_resident.py:        # type: (...) -> 'DeviceResident'
chainer/device_resident.py:        # type: () -> 'DeviceResident'
chainer/device_resident.py:            device  # type: types.DeviceSpec
chainer/device_resident.py:        # type: (...) -> 'DeviceResident'
chainer/function.py:        # type: (Function) -> None
chainer/initializer.py:        # type: (tp.Optional[types.DTypeSpec]) -> None
chainer/initializer.py:        # type: (types.NdArray) -> None
chainer/initializers/__init__.py:    # type: (types.AbstractInitializer, types.ShapeSpec, types.Xp, types.DTypeSpec, types.DeviceSpec) -> types.NdArray  # NOQA
chainer/initializers/__init__.py:    # type: (tp.Optional[types.InitializerSpec]) -> types.AbstractInitializer # NOQA
chainer/iterators/multiprocess_iterator.py:from multiprocessing import sharedctypes  # type: ignore
chainer/link.py:    # type: (tp.Optional[tp.Any]) -> bool
chainer/link.py:    # type: (tp.Optional[tp.Any]) -> tp.Tuple[tp.Optional[types.ShapeSpec], types.DTypeSpec] # NOQA
chainer/link.py:        return value  # type: ignore
chainer/link.py:        # type: (**tp.Any) -> None
chainer/link.py:        # type: () -> collections.OrderedDict[str, chainer.LinkHook]
chainer/link.py:        # type: () -> int
chainer/link.py:        # type: () -> bool
chainer/link.py:        # type: () -> tp.Iterator[None]
chainer/link.py:        # type: (*tp.Any, **tp.Any) -> tp.Any # NOQA
chainer/link.py:        # type: (str, tp.Any) -> None
chainer/link.py:        # type: (str) -> None
chainer/link.py:        # type: (str, tp.Optional[types.ShapeSpec], types.DTypeSpec, tp.Optional[types.InitializerSpec]) -> None # NOQA
chainer/link.py:        # type: (str, tp.Any) -> None
chainer/link.py:        # type: (str) -> None
chainer/link.py:        # type: (str) -> 'Link'
chainer/link.py:        # type: (bool) -> tp.Iterator[chainer.Parameter]
chainer/link.py:        # type: (bool) -> tp.Iterator[tp.Tuple[str, chainer.Parameter]]
chainer/link.py:        # type: (bool) -> tp.Iterator['Link']
chainer/link.py:        # type: (bool) -> tp.Iterator[tp.Tuple[str, 'Link']]
chainer/link.py:        # type: () -> tp.Iterator['Link']
chainer/link.py:        # type: ('Link', bool) -> None
chainer/link.py:        # type: () -> None
chainer/link.py:        # type: () -> None
chainer/link.py:        # type: ('Link') -> None
chainer/link.py:        # type: () -> None
chainer/link.py:        # type: () -> None
chainer/link.py:        # type: () -> bool
chainer/link.py:        # type: (chainer.AbstractSerializer) -> None
chainer/link.py:        # type: (int, str) -> chainer.Sequential
chainer/link.py:        # type: () -> int
chainer/link.py:        # type: (chainer.LinkHook, tp.Optional[str]) -> 'Link'
chainer/link.py:        # type: (str) -> None
chainer/link.py:        # type: (**Link) -> None
chainer/link.py:        # type: (str) -> tp.Any
chainer/link.py:        # type: (str, tp.Any) -> None
chainer/link.py:        # type: (str) -> None
chainer/link.py:        # type: (str, Link) -> None
chainer/link.py:        # type: (str) -> 'Chain'
chainer/link.py:        for name in ret._children:  # type: ignore
chainer/link.py:        return ret  # type: ignore
chainer/link.py:        # type: (bool) -> tp.Iterator[chainer.Parameter]
chainer/link.py:        # type: (bool) -> tp.Iterator[tp.Tuple[str, chainer.Parameter]]
chainer/link.py:        # type: (bool) -> tp.Iterator[Link]
chainer/link.py:        # type: (bool) -> tp.Iterator[tp.Tuple[str, Link]]
chainer/link.py:        # type: () -> tp.Iterator[Link]
chainer/link.py:        # type: (Link, bool) -> None
chainer/link.py:        # type: (Link) -> None
chainer/link.py:        # type: (chainer.AbstractSerializer) -> None
chainer/link.py:        # type: (*Link) -> None
chainer/link.py:        # type: (str, tp.Any) -> None
chainer/link.py:        # type: (tp.Union[int, slice], tp.Union[Link, tp.Iterable[Link]]) -> None # NOQA
chainer/link.py:            for i, c in enumerate(self._children):  # type: ignore
chainer/link.py:        # type: (tp.Union[int, slice]) -> None
chainer/link.py:        # type: (int, Link) -> None
chainer/link.py:        # type: () -> tp.Iterator[Link]
chainer/link.py:        # type: () -> int
chainer/link.py:        # type: (Link) -> None
chainer/link.py:        # type: (str) -> 'ChainList'
chainer/link.py:        return ret  # type: ignore
chainer/link.py:        # type: (bool) -> tp.Iterator[chainer.Parameter]
chainer/link.py:        # type: (bool) -> tp.Iterator[tp.Tuple[str, chainer.Parameter]]
chainer/link.py:        # type: (bool) -> tp.Iterator[Link]
chainer/link.py:        # type: (bool) -> tp.Iterator[tp.Tuple[str, Link]]
chainer/link.py:        # type: () -> tp.Iterator[Link]
chainer/link.py:        # type: (Link, bool) -> None # link is actually a ChainList
chainer/link.py:            child.copyparams(link[idx], copy_persistent)  # type: ignore
chainer/link.py:        # type: (Link) -> None # link is actually a ChainList
chainer/link.py:            child.addgrads(link[idx])  # type: ignore
chainer/link.py:        # type: (chainer.AbstractSerializer) -> None
chainer/link_hook.py:        # type: ('chainer.link.Link', str, tp.Tuple[tp.Any, ...], tp.Dict[str, tp.Any]) -> None # NOQA
chainer/link_hook.py:        # type: ('chainer.link.Link', str, tp.Tuple[tp.Any, ...], tp.Dict[str, tp.Any], tp.Any) -> None # NOQA
chainer/link_hook.py:        # type: () -> LinkHook
chainer/link_hook.py:        # type: (tp.Optional['chainer.link.Link']) -> None
chainer/link_hook.py:        # type: (tp.Optional['chainer.link.Link']) -> None
chainer/link_hook.py:        # type: (_ForwardPreprocessCallbackArgs) -> None
chainer/link_hook.py:        # type: (_ForwardPostprocessCallbackArgs) -> None
chainer/links/connection/linear.py:        # type: (tp.Optional[int], tp.Optional[int], bool, tp.Optional[types.InitializerSpec], tp.Optional[types.InitializerSpec]) -> None # NOQA
chainer/links/connection/linear.py:        # type: (int) -> None
chainer/links/connection/linear.py:        self.W.initialize((self.out_size, in_size))  # type: ignore
chainer/links/connection/linear.py:        # type: (variable.Variable, int) -> variable.Variable
chainer/testing/parameterized.py:        *parameters  # type: tp.Iterable[tp.Dict[str, tp.Any]]
chainer/testing/parameterized.py:    # type: (...) -> tp.Iterator[tp.Dict[str, tp.Any]]
chainer/testing/parameterized.py:    # type: (tp.Sequence[int]) -> tp.Iterator[tp.Tuple[int, ...]]
chainer/types.py:        # type: (NdArray) -> None
chainer/variable.py:        # type: (Variable, tp.Optional[str], **tp.Any) -> None
chainer/variable.py:        # type: (tp.Optional[types.NdArray], **tp.Any) -> None
chainer/variable.py:            self._set_chainerx_array(data, grad)  # type: ignore
chainer/variable.py:        # type: (tp.Optional[chainerx.ndarray], tp.Optional[chainerx.ndarray]) -> None # NOQA
chainer/variable.py:        # type: () -> tp.Optional[types.Xp]
chainer/variable.py:        # type: () -> tp.Optional[types.NdArray]
chainer/variable.py:                    self._data[0].as_grad_stopped())  # type: ignore
chainer/variable.py:        # type: (tp.Optional[types.NdArray]) -> None
chainer/variable.py:                    and (d_old.is_backprop_required()  # type: ignore
chainer/variable.py:                         or d.is_backprop_required())):  # type: ignore
chainer/variable.py:            self._set_chainerx_array(d, None)  # type: ignore
chainer/variable.py:            self._node._update_data_info(d)  # type: ignore # _node doesn't have value when xp is chainerx # NOQA
chainer/variable.py:        # type: () -> tp.Optional[types.NdArray]
chainer/variable.py:        # type: (types.NdArray) -> None
chainer/variable.py:        # type: () -> tp.Optional[types.NdArray]
chainer/variable.py:        # type: (tp.Optional[types.NdArray]) -> None
chainer/variable.py:        # type: () -> tp.Optional["Variable"]
chainer/variable.py:        # type: (tp.Optional["Variable"]) -> None
chainer/variable.py:        # type: (tp.Optional[types.InitializerSpec], tp.Optional[types.ShapeSpec], tp.Optional[str]) -> None # NOQA
chainermn/communicators/communicator_base.py:        # type: () -> bool
docs/source/_napoleon_patch.py:        # type: (unicode) -> List[unicode]

ref. https://www.python.org/dev/peps/pep-0484/

@himkt himkt changed the title Use type hint in link.py Use type hint Oct 4, 2019
@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 6, 2019

[note]
I've noticed type hint for variables is available from Python3.6.
Therefore, I revert my commits and force pushed.

ref. https://www.python.org/dev/peps/pep-0526/


def _parse_attributes_section(self, section):
# type: (unicode) -> List[unicode]
def _parse_attributes_section(self, section: str) -> List[str]:
Copy link
Copy Markdown
Member Author

@himkt himkt Oct 8, 2019

Choose a reason for hiding this comment

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

unicode -> str

I'm not sure if this is correct...:cry::bow:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I looks OK (not so sure though).

@himkt himkt changed the title Use type hint Use type hint for method declaration Oct 9, 2019
@himkt himkt marked this pull request as ready for review October 9, 2019 04:34
@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 9, 2019

I added type hints for method declarations.
@niboshi Could you please take a look? 🙇

@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 9, 2019

Note that CI jobs for Python2.7 (pfn-public-ci/chainer.py27and35.[cg]pu) may fail.

Copy link
Copy Markdown
Member

@niboshi niboshi left a comment

Choose a reason for hiding this comment

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

Thanks!

@niboshi
Copy link
Copy Markdown
Member

niboshi commented Oct 9, 2019

Thank you for the fixes.
I will trigger CIs after chainer/chainer-test#523 will be merged.

@niboshi niboshi added the cat:code-fix Code refactoring that does not change the behavior. label Oct 9, 2019
@niboshi niboshi added the st:blocked-by-another-pr State indicating that another ticket is preventing this ticket from being closed/merged. label Oct 9, 2019
@niboshi
Copy link
Copy Markdown
Member

niboshi commented Oct 10, 2019

Jenkins, test this please

@niboshi niboshi added st:test-and-merge State indicating that pull request is approved by a reviewer and can be merged after CI passes. and removed st:blocked-by-another-pr State indicating that another ticket is preventing this ticket from being closed/merged. labels Oct 10, 2019
@niboshi niboshi added this to the v7.0.0rc1 milestone Oct 10, 2019
@chainer-ci
Copy link
Copy Markdown
Member

Jenkins CI test (for commit a673492, target branch master) failed with status FAILURE.

@niboshi
Copy link
Copy Markdown
Member

niboshi commented Oct 10, 2019

Could you check failures?

@niboshi niboshi added st:awaiting-author State indicating that response is needed from contributors, often authors of pull request. and removed st:test-and-merge State indicating that pull request is approved by a reviewer and can be merged after CI passes. labels Oct 10, 2019
@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 10, 2019

Sure, I'll check it.

@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 10, 2019

Jenkins, test this please

@chainer-ci
Copy link
Copy Markdown
Member

Jenkins CI test (for commit f79ea01, target branch master) succeeded!

@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 10, 2019

flexCI, test this please

@chainer-ci
Copy link
Copy Markdown
Member

Jenkins CI test (for commit f79ea01, target branch master) succeeded!

@himkt
Copy link
Copy Markdown
Member Author

himkt commented Oct 10, 2019

I couldn't launch flexCI...
@niboshi Could you please help me? 🙇

link_hooks[self.name].deleted(None)
del link_hooks[self.name]

def added(self, link: tp.Optional['chainer.link.Link']) -> None:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Errors occurred a string ('chainer.link.Link') was evaluated as an ordinal class in Python3.5. 🤔
I avoided this error by surrounding a whole annotation with whitespace.

@niboshi niboshi removed the st:awaiting-author State indicating that response is needed from contributors, often authors of pull request. label Oct 11, 2019
@niboshi
Copy link
Copy Markdown
Member

niboshi commented Oct 11, 2019

flexCI, test this please

@chainer-ci
Copy link
Copy Markdown
Member

Jenkins CI test (for commit f79ea01, target branch master) failed with status FAILURE.

@niboshi
Copy link
Copy Markdown
Member

niboshi commented Oct 11, 2019

Jenkins, test this please

@chainer-ci
Copy link
Copy Markdown
Member

Jenkins CI test (for commit f79ea01, target branch master) succeeded!

@niboshi
Copy link
Copy Markdown
Member

niboshi commented Oct 11, 2019

Thank you!

@niboshi niboshi merged commit 744c33e into chainer:master Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:code-fix Code refactoring that does not change the behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use Python 3.5 Type Hints

3 participants