drivendataorg / drivendataorg/cloudpathlib

Mypy error creating a `CloudPath` instance

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

Description

## Description

Currently, type-checking a very simple script that uses `cloudpathlib` with Mypy fails with an error.

Script:
```python
from cloudpathlib import CloudPath

path = CloudPath("s3://abc/xyz")
```

Mypy error:
```python
test.py:3: error: Cannot instantiate abstract class "CloudPath" with abstract attributes "drive", "is_dir", "is_file", "mkdir" and "touch" [abstract]
```

## Cause

I believe this is a result of the fact that [Mypy doesn't yet understand the `__call__` method on metaclasses](https://github.com/python/mypy/issues/14122).

## Workarounds

### Specific Subclass

One way to avoid this error is for end users to directly use implementation-specific `CloudPath` subclasses. The following script produces no Mypy errors:
```python
from cloudpathlib import S3Path

path = S3Path("s3://abc/xyz")
```

This slightly limits portability of end user scripts, but is one possible way to avoid the error.

### Ignore Directive

The error can also be ignored with a `# type: ignore[abstract]` directive:
```python
from cloudpathlib import CloudPath

path = CloudPath("s3://abc/xyz") # type: ignore[abstract]
```

### New Method

Lastly, some changes could be made to how `CloudPath` instances are constructed to work around this issue. However, this would introduce a new way of instantiating `CloudPath` instances, which could be confusing.

One option would be to add a `CloudPath.from_url` class method, which would just `return CloudPath(url)`. This way, the Mypy error could be ignored in the `cloudpathlib` codebase rather than user code.

Another option would be to implement cloud provider-specific dispatching in the new `CloudPath.from_url` class method, then have `CloudPathMeta.__call__` delegate to `CloudPath.from_url` for its implementation. This wouldn't require any new ignore directives in the `cloudpathlib` codebase.

In any case, I didn't find any issues describing this behavior elsewhere, so hopefully this is helpful to others who run into this same issue in the future 🙂

Thanks for maintaining cloudpathlib! 😄

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.