Skip to content

Managing Deprecation using decorators #1585

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

Merged
merged 16 commits into from
Feb 6, 2021

Conversation

Devanshu24
Copy link
Contributor

@Devanshu24 Devanshu24 commented Jan 27, 2021

This is a very stripped down version of the code, I have not written any tests yet. This is primarily for me to check if I am going in the correct direction. So please do tell everything that needs improvement/changing.

Fixes #1479

Description:
Implemented till now

  • Make functions deprecated using the @deprecated decorator
  • Add arguments to the @deprecated decorator to customize it for each function
  • The deprecation messages also reflect in the documentation (written in Sphinx compatible format)

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

Thank you to @ydcjeff for giving the idea to add the version update information :)

@Devanshu24 Devanshu24 force-pushed the deprecation-decorators branch from 7367e2a to 3f3da71 Compare January 27, 2021 17:57
@sdesrozis
Copy link
Contributor

@Devanshu24 Thank you, it looks very good to me!

@Devanshu24
Copy link
Contributor Author

Devanshu24 commented Jan 28, 2021

Thank you for your review @sdesrozis!
So since no one suggested any changes I hope this is a good way to proceed.
I plan to add the following decorators (and their tests):

  • @deprecated (the one currently shown)
  • @version_added
  • @version_changed
  • @change_default_value changes the default value of an argument
  • @remove_arg
  • @deprecate_kwarg

I found these from the link @vfdev-5 gave in #1479 (this one to be precise) and little googling for similar stuff. However, I am not sure which of these would actually be helpful. I am more than happy to do all of them but I felt that if there is even one of them which will never be used it will just be some dead code.

So I am looking for your suggestions :)

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jan 28, 2021

Thanks for the suggestions @Devanshu24 !
Let's start with @deprecated, as for the others let's see a bit later. Personnally, I do not have any strong opnion about other decorators.

@sdesrozis
Copy link
Contributor

I agree @vfdev-5 about others decorators. Let's see first using @deprecated :) and we keep in mind other decorators !

@Devanshu24 Devanshu24 force-pushed the deprecation-decorators branch from 8937c09 to a2ff1e9 Compare January 31, 2021 13:19
@Devanshu24
Copy link
Contributor Author

I have made some minimal changes to the @deprecated function to cover some edge cases. I have also added tests to test most of the use-cases (I hope nothing important has been left out :) )

If there are any changes that need to be made please let me know :)

PS: I have not removed the DeprecationWarning from the setup_any_logging function as I was not sure what to enter in the body of the function.

@Devanshu24 Devanshu24 marked this pull request as ready for review January 31, 2021 16:22
@Devanshu24 Devanshu24 force-pushed the deprecation-decorators branch from bebb821 to a310730 Compare January 31, 2021 16:28
@vfdev-5 vfdev-5 requested a review from sdesrozis January 31, 2021 19:32
Copy link
Contributor

@sdesrozis sdesrozis left a comment

Choose a reason for hiding this comment

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

It looks good to me !! @Devanshu24 Thank you !

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jan 31, 2021

@sdesrozis should we also use in the code this decorator ?

@Devanshu24
Copy link
Contributor Author

@vfdev-5 Any chages/additions needed?

Also if you could guide me on this :)

PS: I have not removed the DeprecationWarning from the setup_any_logging function as I was not sure what to enter in the body of the function.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 1, 2021

@Devanshu24 I think we should update setup_any_logging and use your decorator to show an example of usage.

@Devanshu24
Copy link
Contributor Author

Devanshu24 commented Feb 1, 2021

@Devanshu24 I think we should update setup_any_logging and use your decorator to show an example of usage.

Okay, and in the function body should I just add a pass statement?

EDIT: As the current one with raise DeprecationWarning() is actually causing an exception

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 1, 2021

@Devanshu24 maybe we can configure the decorator such that it could either raise DeprecationWarning or use warning.warn ?

@Devanshu24
Copy link
Contributor Author

@Devanshu24 maybe we can configure the decorator such that it could either raise DeprecationWarning or use warning.warn ?

Oh okay, I thought that as long as the function is still in the deprecated state (i.e. not yet removed) we wouldn't want the code to cause an exception and interrupt the program.
If you say so I could also add an option that raises an Exception?
So the options would be,

  • No Exception, No Warning
  • Just Warning, No Exception
  • Raise Exception

PS: I learnt that raise DeprecationWarning() causes an exception from here

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 1, 2021

Well, you are right that usually we do not raise exceptions on deprecated functions. It actually may depend to security criticity of the function (for example, see yaml.load: yaml/pyyaml#292). In our case, usage of setup_any_logging is not that critical, we just removed the implementation, so showing a warning and doing nothing is not an option for our code neither...

A bit details for your table, if function is decorated with deprecated:

  • Deprecation in docs, Just Warning, No Exception
  • Deprecation in docs, Raise Exception
  • No Exception, No Warning (= no decorator, right?)

@Devanshu24
Copy link
Contributor Author

Devanshu24 commented Feb 1, 2021

Well, you are right that usually we do not raise exceptions on deprecated functions. It actually may depend to security criticity of the function (for example, see yaml.load: yaml/pyyaml#292). In our case, usage of setup_any_logging is not that critical, we just removed the implementation, so showing a warning and doing nothing is not an option for our code neither...

Oh okay, got to learn something new, thanks! :D

A bit details for your table, if function is decorated with deprecated:

  • Deprecation in docs, Just Warning, No Exception
  • Deprecation in docs, Raise Exception
  • No Exception, No Warning (= no decorator, right?)

Yeah right, that makes sense, I thought that maybe we would want to have a case in which we just say it is deprecated in the docs, but not raise any warnings or exceptions. But your point makes more sense

I'll make the edits, and now we will have just two options:

  • Deprecation in docs, Just Warning, No Exception
  • Deprecation in docs, Raise Exception

@Devanshu24 Devanshu24 force-pushed the deprecation-decorators branch from c1db19f to a310730 Compare February 1, 2021 18:41
* Make functions deprecated using the `@deprecated` decorator
* Add arguments to the @deprecated decorator to customize it for each function
* Replaced the `raise` keyword with added `warnings`
* Added tests several possibilities of the decorator usage
@Devanshu24 Devanshu24 force-pushed the deprecation-decorators branch from cd9daa1 to dd3a1c4 Compare February 1, 2021 18:44
@Devanshu24
Copy link
Contributor Author

I am getting the following error in the unit tests and during deploying the docs

NameError: name 'TextIO' is not defined

I think the addition was made in #1601, to be precise in the line

stream: Optional[TextIO] = None,

I haven't understood the error yet, thought I would ask if one of you know what it is, if not I'll start looking :)

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 3, 2021

Oh, this looks like a bug. @sdesrozis any ideas ?

@Devanshu24 at which moment exactly do you see that, on which command ?

@Devanshu24
Copy link
Contributor Author

Okay I figured it. It was a small thing, TextIO wasn't imported from typing

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 3, 2021

Okay I figured it. It was a small thing, TextIO wasn't imported from typing

Normally, it should be imported here :

from typing import Any, Callable, Optional, TextIO, Tuple, Type, Union, cast

@Devanshu24
Copy link
Contributor Author

@Devanshu24 at which moment exactly do you see that, on which command ?

pytest tests/ignite/test_utils.py -s -vvv -k test_deprecated

and inside the docs folder with the following command

make html

Although after importing it both the commands work as expected :)

@Devanshu24
Copy link
Contributor Author

I am not sure why the mypy test keeps failing, as they are working locally for me.
Trying to debug what is happening


Output of running it locally

$ mypy --config-file mypy.ini
Success: no issues found in 86 source files

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 3, 2021

@Devanshu24 are using the same version ?

* Change the test to work with the `@deprecated` decorator
@Devanshu24
Copy link
Contributor Author

@Devanshu24 are using the same version ?

It is the same as the one on the CI i.e. mypy 0.800
Although 2d49a48 fixes the mypy errors, the changes made in that commit pass tests on CI and also does not interfere with the local tests (which were already passing)

@Devanshu24
Copy link
Contributor Author

I think all tests are now passing (except codecov, but the uncovered line will be uncovered)
Please have a look at your convenience :)

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @Devanshu24 !
Few other comments to address, please

@Devanshu24
Copy link
Contributor Author

Devanshu24 commented Feb 4, 2021

@vfdev-5 mypy is again failing on CI. It still passes locally though (EDIT: Can you also test is locally)
It is because of the incorrect placement of # type: ignore

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 4, 2021

@Devanshu24 I have to check that locally from my side.

@gruebel do you have an idea about why mypy is failing ?

@Devanshu24
Copy link
Contributor Author

Devanshu24 commented Feb 4, 2021

Okayy I think I get it, the Python3.8 CI is still passing, which is my local python version too
There is some issue with 3.7 and 3.6

@gruebel
Copy link
Contributor

gruebel commented Feb 4, 2021

@vfdev-5 I will take a look today, I also left some comments on the typing.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 4, 2021

Thanks a lot @gruebel !

@gruebel
Copy link
Contributor

gruebel commented Feb 4, 2021

So, I digged into the problem it seems like to be an issue with mypy and how it handles multiline functions/decorators. The decorator belongs to the function, so mypy gets messed up and blames the first line. Interestingly it was fixed for Python 3.8, but not lower python/mypy#3251.

So, to make mypy happy it needs to be like @Devanshu24 implemented it before

@deprecated(
    "0.4.0",  # type: ignore
    "0.6.0",
    ["Please use instead: setup_tb_logging, setup_visdom_logging or setup_mlflow_logging etc."],
    raise_exception=True,
)
def setup_any_logging(logger, logger_module, trainer, optimizers, evaluators, log_every_iters) -> None:
    pass

or just add some type hints and then you can remove the # type: ignore part 😄

@deprecated(
    "0.4.0",
    "0.6.0",
    ["Please use instead: setup_tb_logging, setup_visdom_logging or setup_mlflow_logging etc."],
    raise_exception=True,
)
def setup_any_logging(
    logger: BaseLogger,
    logger_module: Any,
    trainer: Engine,
    optimizers: Optional[Union[Optimizer, Dict[str, Optimizer], Dict[None, Optimizer]]],
    evaluators: Optional[Union[Engine, Dict[str, Engine]]],
    log_every_iters: int,
) -> None:
    pass

I would prefer just to add the types, which approach do you prefer @vfdev-5 ?

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Feb 5, 2021

Thank a lot @gruebel for investigating the issue 💪 !
Yes, I also prefer the second solution with type hints as you suggested.
@Devanshu24 could you please apply those changes .

@Devanshu24
Copy link
Contributor Author

Thanks @gruebel !
Sure @vfdev-5 will get them done today!

@Devanshu24
Copy link
Contributor Author

Made the suggested changes, please have a look at your convenience :)

vfdev-5 and others added 4 commits February 5, 2021 22:01
pytorch#1612)

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5.0 to v0.4.3

* Update ignite/contrib/metrics/regression/canberra_metric.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/manhattan_distance.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/r2_score.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/handlers/checkpoint.py

Co-authored-by: vfdev <[email protected]>

* address PR comments

Co-authored-by: vfdev <[email protected]>
Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Great work @Devanshu24 ! Thanks !

@vfdev-5 vfdev-5 merged commit 29a436f into pytorch:master Feb 6, 2021
fco-dv added a commit to fco-dv/ignite that referenced this pull request Feb 15, 2021
* Starter code for managing deprecation

* Make functions deprecated using the `@deprecated` decorator
* Add arguments to the @deprecated decorator to customize it for each function

* Improve `@deprecated` decorator and add tests

* Replaced the `raise` keyword with added `warnings`
* Added tests several possibilities of the decorator usage

* Removing the test deprecation to check tests

* Add static typing, fix mypy errors

* Make `@deprecated` to raise Exceptions or Warning

* The `@deprecated` decorator will now always emit warning unless explicitly asked to raise an Exception

* Fix mypy errors

* Fix mypy errors (hopefully)

* Fix the test `test_deprecated_setup_any_logging`

* Change the test to work with the `@deprecated` decorator

* Change to snake_case, handle mypy ignores

* Improve Type Annotations

* Update common.py

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5… (pytorch#1612)

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5.0 to v0.4.3

* Update ignite/contrib/metrics/regression/canberra_metric.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/manhattan_distance.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/r2_score.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/handlers/checkpoint.py

Co-authored-by: vfdev <[email protected]>

* address PR comments

Co-authored-by: vfdev <[email protected]>

* `version` -> version

Co-authored-by: vfdev <[email protected]>
Co-authored-by: François COKELAER <[email protected]>
Co-authored-by: Sylvain Desroziers <[email protected]>
vfdev-5 added a commit that referenced this pull request Feb 21, 2021
* Recall/Precision metrics for ddp : average == false and multilabel == true

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5… (#1612)

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5.0 to v0.4.3

* Update ignite/contrib/metrics/regression/canberra_metric.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/manhattan_distance.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/r2_score.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/handlers/checkpoint.py

Co-authored-by: vfdev <[email protected]>

* address PR comments

Co-authored-by: vfdev <[email protected]>

* added TimeLimit handler with its test and doc (#1611)

* added TimeLimit handler with its test and doc

* fixed documentation

* fixed docstring and formatting

* flake8 fix trailing whitespace :)

* modified class logger , default value and tests

* changed rounding to nearest integer

* tests refactored , docs modified

* fixed default value , removed global logger

* fixing formatting

* Added versionadded

* added test for engine termination

Co-authored-by: vfdev <[email protected]>

* Update handlers to use setup_logger (#1617)

* Fixes #1614
- Updated handlers EarlyStopping and TerminateOnNan
- Replaced `logging.getLogger` with `setup_logger` in the mentioned handlers

* Updated `TimeLimit` handler.
Replaced use of `logger.getLogger` with `setup_logger` from `ignite.utils`

Co-authored-by: Pradyumna Rahul K <[email protected]>
Co-authored-by: Sylvain Desroziers <[email protected]>

* Managing Deprecation using decorators (#1585)

* Starter code for managing deprecation

* Make functions deprecated using the `@deprecated` decorator
* Add arguments to the @deprecated decorator to customize it for each function

* Improve `@deprecated` decorator and add tests

* Replaced the `raise` keyword with added `warnings`
* Added tests several possibilities of the decorator usage

* Removing the test deprecation to check tests

* Add static typing, fix mypy errors

* Make `@deprecated` to raise Exceptions or Warning

* The `@deprecated` decorator will now always emit warning unless explicitly asked to raise an Exception

* Fix mypy errors

* Fix mypy errors (hopefully)

* Fix the test `test_deprecated_setup_any_logging`

* Change the test to work with the `@deprecated` decorator

* Change to snake_case, handle mypy ignores

* Improve Type Annotations

* Update common.py

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5… (#1612)

* For v0.4.3 - Add more versionadded, versionchanged tags - Change v0.5.0 to v0.4.3

* Update ignite/contrib/metrics/regression/canberra_metric.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/manhattan_distance.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/contrib/metrics/regression/r2_score.py

Co-authored-by: vfdev <[email protected]>

* Update ignite/handlers/checkpoint.py

Co-authored-by: vfdev <[email protected]>

* address PR comments

Co-authored-by: vfdev <[email protected]>

* `version` -> version

Co-authored-by: vfdev <[email protected]>
Co-authored-by: François COKELAER <[email protected]>
Co-authored-by: Sylvain Desroziers <[email protected]>

* Create documentation.md

* Distributed tests on Windows should be skipped until fixed. (#1620)

* modified CONTRIBUTING.md

* bash instead of sh

* Added Checkpoint.get_default_score_fn (#1621)

* Added Checkpoint.get_default_score_fn to simplify best_model_handler creation

* Added score_sign argument

* Updated docs

* Update about.rst

* Update pre-commit hooks and CONTRIBUTING.md (#1622)

* Change pre-commit config and CONTRIBUTING.md

- Update hook versions
- Remove seed-isort-config
- Add black profile to isort

* Fix files based on new pre-commit config

* Add meaningful exclusions to prettier

- Also update actions workflow files to match local pre-commit

* added requirements.txt and updated readme.md (#1624)

* added requirements.txt and updated readme.md

* Update examples/contrib/cifar10/README.md

Co-authored-by: vfdev <[email protected]>

* Update examples/contrib/cifar10/requirements.txt

Co-authored-by: vfdev <[email protected]>

Co-authored-by: vfdev <[email protected]>

* Replace relative paths with raw.githubusercontent (#1629)

* Updated cifar10 example (#1632)

* Updates for cifar10 example

* Updates for cifar10 example

* More updates

* Updated code

* Fixed code-formatting

* Fixed failling CI and typos for cifar10 examples (#1633)

* Updates for cifar10 example

* Updates for cifar10 example

* More updates

* Updated code

* Fixed code-formatting

* Fixed typo and failing CI

* Fixed hvd spawn fail and better synced qat code

* Removed temporary hack to install pth 1.7.1 (#1638)

- updated default pth image for gpu tests
- updated TORCH_CUDA_ARCH_LIST
- fixed /merge -> /head in trigger ci pipeline

* [docker] Pillow -> Pillow-SIMD (#1509) (#1639)

* [docker] Pillow -> Pillow-SIMD (#1509)

* [docker] Pillow -> Pillow-SIMD

* replace pillow with pillow-simd in base docker files

* chore(docker): apt-get autoremove after pillow-simd installation

* apt-get install at once, autoremove g++

* install g++ in pillow installation layer

Co-authored-by: Sylvain Desroziers <[email protected]>

* Fix g++ install issue

Co-authored-by: Jeff Yang <[email protected]>
Co-authored-by: Sylvain Desroziers <[email protected]>

* Fix multinode tests script (#1631)

* fix run_multinode_tests_in_docker.sh : run tests with docker python version

* add missing modules

* build an image with test env and add 'nnodes' 'nproc_per_node' 'gpu' as parameters

* #1615 : change nproc_per_node default to 4

* #1615 : fix for gpu enabled tests / container rm step at the end of the script

* add xfail decorator for tests/ignite/engine/test_deterministic.py::test_multinode_distrib_cpu

* fix script gpu_options

* add default tol=1e-6 for _test_distrib_compute_on_criterion

* fix for "RuntimeError: trying to initialize the default process group twice!"

* tolerance for test_multinode_distrib_cpu case only

* fix assert None error

* autopep8 fix

Co-authored-by: vfdev <[email protected]>
Co-authored-by: Sylvain Desroziers <[email protected]>
Co-authored-by: fco-dv <[email protected]>

* remove warning for average=False and is_multilabel=True

* update docstring and {precision, recall} tests according to test_multilabel_input_NCHW

Co-authored-by: vfdev <[email protected]>
Co-authored-by: Ahmed Omar <[email protected]>
Co-authored-by: Pradyumna Rahul <[email protected]>
Co-authored-by: Pradyumna Rahul K <[email protected]>
Co-authored-by: Sylvain Desroziers <[email protected]>
Co-authored-by: Devanshu Shah <[email protected]>
Co-authored-by: Debojyoti Chakraborty <[email protected]>
Co-authored-by: Jeff Yang <[email protected]>
Co-authored-by: fco-dv <[email protected]>
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.

Improve deprecation messages
5 participants