drivendataorg / drivendataorg/cloudpathlib

Use `AnyPath` instead of `to_anypath` as constructor by fixing type issues

Open
#146 4 comments 0 reactions 0 assignees View on GitHub
bug typing
Dominant language
Python
Stars
628
Forks
88
Avg merge
17h 28m
Merged PRs (30d)
2

Description

When implementing #142 I encountered a tricky type annotation scenario and used a workaround.

The `AnyPath` constructor instead of `to_anypath` in the code referenced below runs fine, but `mypy` complains. We should determine the right type annotations to use `AnyPath(p)` instead of `to_anypath`.

---------------

I couldn't use the `AnyPath` constructor

For some reason, this was the only way I could make `mypy` happy after the call here. Maybe you've got a nice solution:

https://github.com/drivendataorg/cloudpathlib/blob/8dca2edf4f950ee1452b4939e51a480f6b79b4a4/cloudpathlib/cloudpath.py#L695-L697

--------

If I change that line to `anypath.AnyPath(destination)`, I see these errors even if I `# type: ignore` that line.

```
mypy cloudpathlib
cloudpathlib/cloudpath.py:699: error: Item "str" of "Union[str, _PathLike[Any], CloudPath]" has no attribute "exists"
cloudpathlib/cloudpath.py:699: error: Item "_PathLike[Any]" of "Union[str, _PathLike[Any], CloudPath]" has no attribute "exists"
cloudpathlib/cloudpath.py:699: error: Item "str" of "Union[str, _PathLike[Any], CloudPath]" has no attribute "is_file"
cloudpathlib/cloudpath.py:699: error: Item "_PathLike[Any]" of "Union[str, _PathLike[Any], CloudPath]" has no attribute "is_file"
cloudpathlib/cloudpath.py:704: error: Item "str" of "Union[str, _PathLike[Any], CloudPath]" has no attribute "mkdir"
cloudpathlib/cloudpath.py:704: error: Item "_PathLike[Any]" of "Union[str, _PathLike[Any], CloudPath]" has no attribute "mkdir"
cloudpathlib/cloudpath.py:716: error: Incompatible return value type (got "Union[str, _PathLike[Any], CloudPath]", expected "Union[Path, CloudPath]")
Found 7 errors in 1 file (checked 24 source files)
```

_Originally posted by @pjbull in https://github.com/drivendataorg/cloudpathlib/pull/142#discussion_r635764364_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.