Skip to content

Commit

Permalink
typings
Browse files Browse the repository at this point in the history
  • Loading branch information
moosetraveller committed Sep 1, 2023
1 parent cd81c78 commit fb7d781
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/aputil/fc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@
"""

import uuid
import arcpy
import arcpy, arcpy.management

from contextlib import contextmanager

from collections.abc import Generator
from typing import Union

from .typings import FeatureClassType

__all__ = ["count", "use_memory"]

@contextmanager
def use_memory(name: str = None) -> str:
def use_memory(name: str = None) -> Generator[str, None, None]:

# Context Manager typings:
# https://stackoverflow.com/a/70277752/42659
# https://adamj.eu/tech/2021/07/04/python-type-hints-how-to-type-a-context-manager
# https://github.com/python/typeshed/issues/2772

# random name if no name is given
name = name or rf"memory\fc_{uuid.uuid4().hex}"
Expand All @@ -68,7 +74,7 @@ def use_memory(name: str = None) -> str:
finally:
arcpy.management.Delete(name)

def count(feature_class: Union[str, FeatureClassType]) -> int:
def count(feature_class: FeatureClassType) -> int:
""" Returns the numbers of features in given feature class as an int value.
Introduced to overcome dealing with `arcpy.management.GetCount`'s
return value of type `arcpy.arcobjects.arcobjects.Result`."""
Expand Down
4 changes: 2 additions & 2 deletions src/aputil/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

from .typings import FeatureClassType # backward compatibility
from .fc import count # backward compatibility
from .typings import FeatureClassType # backward compatibility, moved to aputil.typings
from .fc import count # backward compatibility, moved to aputil.fc

__all__ = ["FeatureClassType", "count"]
2 changes: 1 addition & 1 deletion src/aputil/typings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

from typing import TypeVar

FeatureClassType = TypeVar("FeatureClassType") # for documenting reason only
FeatureClassType = TypeVar("FeatureClassType") # for documenting reason

__all__ = ["FeatureClassType"]

0 comments on commit fb7d781

Please sign in to comment.