Skip to content

#2418 replace os.path by pathlib.Path#2461

Merged
vfdev-5 merged 30 commits intopytorch:masterfrom
yuta0821:feature/#2418_replace_os_path_by_Path
Feb 20, 2022
Merged

#2418 replace os.path by pathlib.Path#2461
vfdev-5 merged 30 commits intopytorch:masterfrom
yuta0821:feature/#2418_replace_os_path_by_Path

Conversation

@yuta0821
Copy link
Copy Markdown
Contributor

Fixes #2418

Description:

  • Environment
    In order to construct the test environment, I used docker image continuumio/miniconda3 and install necessary dependencies, following CONTRIBUTING.md.

  • Update code

    • I have replaced all os.path.exists(folder), os.path.join(a, b), os.makedirs(folder) by Path(folder).exists(), Path(a) / b, Path.mkdir(folder, parents=True), respectively.
    • I have updated all folder/files being str to Union[str, Path] and make all folder/file-like attributes as Path objects.
      • example
        • filename: str -> filename: Union[str, Path]
        • assert isinstance(fname, str) -> assert isinstance(fname, str) or isinstance(fname, Path)
  • Test the code

    • I ran the following command
      pytest --tx 4*popen//python=python --cov ignite --cov-report term-missing --cov-report xml -vvv tests "${skip_distrib_opt[@]}"
    • results
      I got an error due to this replacement in two test files.
      /opt/conda/envs/pytorch-ignite-dev/lib/python3.8/urllib/parse.py:111: in _decode_args return tuple(x.decode(encoding, errors) if x else '' for x in args)
      => AttributeError: 'PosixPath' object has no attribute 'decode'
      • /tests/ignite/contrib/handlers/test_mlflow_logger.py
        • line253: mlflow_logger = MLflowLogger(tracking_uri=Path(dirname) / "mlruns")
        • line295: with MLflowLogger(Path(dirname) / "mlruns") as mlflow_logger:
        • line327: with MLflowLogger(Path(dirname) / "mlruns") as mlflow_logger:
      • /tests/ignite/contrib/engines/test_common.py
        • line488: mlf_logger = _test_setup_logging(
    • my proposition
      I think it would be better to use the original code using os.path without this replacement for the part that causes the error.
  • Code Format
    In order to check code style, I ran the following commands.
    isort . --settings setup.cfg
    black . --config pyproject.toml

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)

@github-actions github-actions bot added docs examples Examples module: handlers Core Handlers module labels Feb 14, 2022
Copy link
Copy Markdown
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 a lot for the PR @yuta0821 ! Great work ! I left few comments to improve the code. As there is a lot of modified files, it can take time to review everything. A solution to that could be splitting the PR into mutiple ones, but not required...

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 14, 2022

I got an error due to this replacement in two test files.
/opt/conda/envs/pytorch-ignite-dev/lib/python3.8/urllib/parse.py:111: in _decode_args return tuple(x.decode(encoding, errors) if x else '' for x in args)
=> AttributeError: 'PosixPath' object has no attribute 'decode'

/tests/ignite/contrib/handlers/test_mlflow_logger.py
    line253: mlflow_logger = MLflowLogger(tracking_uri=Path(dirname) / "mlruns")

@yuta0821 this is OK due to the fact that mlflow uses URI for tracking_uri which could be a local path or URL to a server. Let's keep tracking_uri as str.

@yuta0821
Copy link
Copy Markdown
Contributor Author

yuta0821 commented Feb 15, 2022

@vfdev-5
I modify some code you commented above.
I ran the following tests, and they all passed.
pytest --tx 4*popen//python=python --cov ignite --cov-report term-missing --cov-report xml -vvv tests "${skip_distrib_opt[@]}"
I would appreciate it if you could check them.

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 17, 2022

@yuta0821 could you please resolve conflicting files

Copy link
Copy Markdown
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.

@yuta0821 I checked a bit more in details the PR and left other comments how we can simplify the code

@yuta0821
Copy link
Copy Markdown
Contributor Author

yuta0821 commented Feb 18, 2022

@vfdev-5

I checked a bit more in details the PR and left other comments how we can simplify the code.

Thank you for your reply and a lot of comments that helps me very much.
I modified some codes to treat as a path object and resolved conflict.
I ran the following tests, and they all passed.

pytest --tx 4*popen//python=python --cov ignite --cov-report term-missing --cov-report xml -vvv tests "${skip_distrib_opt[@]}"

I would appreciate it if you could confirm.

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 18, 2022

pytest --tx 4*popen//python=python --cov ignite --cov-report term-missing --cov-report xml -vvv tests "${skip_distrib_opt[@]}"

I would appreciate it if you could confirm.

@yuta0821 you can run all tests with bash tests/run_cpu_tests.sh (https://github.com/pytorch/ignite/blob/master/CONTRIBUTING.md#run-tests)

Copy link
Copy Markdown
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 a lot for the update @yuta0821 !
Just few nits to pick and should be good to go.

@yuta0821
Copy link
Copy Markdown
Contributor Author

@yuta0821 you can run all tests with bash tests/run_cpu_tests.sh

@vfdev-5 Thank you for your advise.
At first, I cannot run shell scripts probably due to the windows environment.
However, I was able to run tests/run_cpu_tests.sh after executing the command dos2unix and passed all tests.

I modified some codes to be match with your suggested change.
I would appreciate it if you could confirm.

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 19, 2022

@yuta0821 sorry I tried to improve type hints but I have to come back to what you did. Sorry for the noise.

I restart the CI and if it is successful I think we can merge this PR

EDIT: I'll fix the issues reported by the CI and move forward this PR

@vfdev-5 vfdev-5 removed the docs label Feb 19, 2022
@yuta0821
Copy link
Copy Markdown
Contributor Author

@yuta0821 sorry I tried to improve type hints but I have to come back to what you did. Sorry for the noise.

Thank you for your try to improve type hints very much.

I restart the CI and if it is successful I think we can merge this PR
EDIT: I'll fix the issues reported by the CI and move forward this PR

If there is anything I can do for this PR, please let me know.

@vfdev-5 vfdev-5 enabled auto-merge (squash) February 20, 2022 09:33
@vfdev-5 vfdev-5 merged commit 0885113 into pytorch:master Feb 20, 2022
@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 20, 2022

Thanks for the fix @yuta0821
I merged the PR !

@yuta0821
Copy link
Copy Markdown
Contributor Author

@vfdev-5
Thank you for a lot of advice and improvements !

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 20, 2022

Hope you enjoyed and learnt while contributing :)

@yuta0821 yuta0821 deleted the feature/#2418_replace_os_path_by_Path branch February 20, 2022 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Examples module: handlers Core Handlers module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace all os.path in ignite and tests by pathlib.Path

2 participants