Skip to content

Commit

Permalink
[#3206]improvement(client-python): Add Black for client-python (#3254)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

* Add Black as code formatter, it enforce the coding style with 
	1. trailing commas and whitespaces
	2. unified quotation marks(")
	3. new lines
	4. max line length
	5. indents
* Aligned with Pylint Rules
* Add Black into Gradle and form a code formatting pipeline (pipInstall
-> Black -> Pylint), this pipeline will run implicitly in `build` and
`test` gradle tasks.

> Note that Black still can't format long entire string exceeding `max
line length` without enabling unstable features, please handle long
strings with caution and make Pylint to ignore them if they are really
necessary.

### Why are the changes needed?

Fix: #3206, #3203 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

No

---------

Co-authored-by: TimWang <[email protected]>
  • Loading branch information
noidname01 and TimWang authored May 10, 2024
1 parent bd28288 commit a88a361
Show file tree
Hide file tree
Showing 59 changed files with 859 additions and 371 deletions.
17 changes: 15 additions & 2 deletions clients/client-python/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ tasks {
args = listOf("install", "-e", ".[dev]")
}

val black by registering(VenvTask::class) {
dependsOn(pipInstall)
venvExec = "black"
args = listOf("./gravitino", "./tests")
}

val pylint by registering(VenvTask::class) {
dependsOn(pipInstall)
mustRunAfter(black)
venvExec = "pylint"
args = listOf("./gravitino", "./tests")
}
Expand All @@ -50,7 +58,6 @@ tasks {
gravitinoServer("start")
}

dependsOn(pipInstall, pylint)
venvExec = "python"
args = listOf("-m", "unittest")
workingDir = projectDir.resolve(".")
Expand All @@ -67,7 +74,6 @@ tasks {
}

val build by registering(VenvTask::class) {
dependsOn(pipInstall, pylint)
}

val clean by registering(Delete::class) {
Expand All @@ -79,4 +85,11 @@ tasks {
deleteCacheDir("__pycache__")
}
}

matching {
it.name.endsWith("envSetup")
}.all {
// add install package and code formatting before any tasks
finalizedBy(pipInstall, black, pylint)
}
}
1 change: 1 addition & 0 deletions clients/client-python/gravitino/api/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import ABC, abstractmethod
from datetime import datetime

Expand Down
1 change: 1 addition & 0 deletions clients/client-python/gravitino/api/auditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import ABC, abstractmethod

from gravitino.api.audit import Audit
Expand Down
18 changes: 12 additions & 6 deletions clients/client-python/gravitino/api/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import abstractmethod
from enum import Enum
from typing import Dict, Optional
Expand All @@ -14,6 +15,7 @@ class Catalog(Auditable):
"""The interface of a catalog. The catalog is the second level entity in the gravitino system,
containing a set of tables.
"""

class Type(Enum):
"""The type of the catalog."""

Expand Down Expand Up @@ -92,9 +94,11 @@ def as_schemas(self) -> SupportsSchemas:
Returns:
The {@link SupportsSchemas} if the catalog supports schema operations.
"""
raise UnsupportedOperationException("Catalog does not support schema operations")
raise UnsupportedOperationException(
"Catalog does not support schema operations"
)

def as_table_catalog(self) -> 'TableCatalog':
def as_table_catalog(self) -> "TableCatalog":
"""
Raises:
UnsupportedOperationException if the catalog does not support table operations.
Expand All @@ -104,17 +108,19 @@ def as_table_catalog(self) -> 'TableCatalog':
"""
raise UnsupportedOperationException("Catalog does not support table operations")

def as_fileset_catalog(self) -> 'FilesetCatalog':
def as_fileset_catalog(self) -> "FilesetCatalog":
"""
Raises:
UnsupportedOperationException if the catalog does not support fileset operations.
Returns:
the FilesetCatalog if the catalog supports fileset operations.
"""
raise UnsupportedOperationException("Catalog does not support fileset operations")
raise UnsupportedOperationException(
"Catalog does not support fileset operations"
)

def as_topic_catalog(self) -> 'TopicCatalog':
def as_topic_catalog(self) -> "TopicCatalog":
"""
Returns:
the {@link TopicCatalog} if the catalog supports topic operations.
Expand All @@ -126,4 +132,4 @@ def as_topic_catalog(self) -> 'TopicCatalog':


class UnsupportedOperationException(Exception):
pass
pass
1 change: 1 addition & 0 deletions clients/client-python/gravitino/api/catalog_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import ABC


Expand Down
2 changes: 2 additions & 0 deletions clients/client-python/gravitino/api/fileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import abstractmethod
from enum import Enum
from typing import Optional, Dict
Expand All @@ -20,6 +21,7 @@ class Fileset(Auditable):
Fileset defines the basic properties of a fileset object. A catalog implementation
with FilesetCatalog should implement this interface.
"""

class Type(Enum):
"""An enum representing the type of the fileset object."""

Expand Down
11 changes: 6 additions & 5 deletions clients/client-python/gravitino/api/fileset_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import ABC
from dataclasses import field

Expand Down Expand Up @@ -65,7 +66,7 @@ def remove_property(property):
class RenameFileset:
"""A fileset change to rename the fileset."""

_new_name: str = field(metadata=config(field_name='new_name'))
_new_name: str = field(metadata=config(field_name="new_name"))

def __init__(self, new_name):
self._new_name = new_name
Expand Down Expand Up @@ -113,7 +114,7 @@ def __str__(self):
class UpdateFilesetComment:
"""A fileset change to update the fileset comment."""

_new_comment: str = field(metadata=config(field_name='new_comment'))
_new_comment: str = field(metadata=config(field_name="new_comment"))

def __init__(self, new_comment):
self._new_comment = new_comment
Expand Down Expand Up @@ -161,8 +162,8 @@ def __str__(self):
class SetProperty:
"""A fileset change to set the property and value for the fileset."""

_property: str = field(metadata=config(field_name='property'))
_value: str = field(metadata=config(field_name='value'))
_property: str = field(metadata=config(field_name="property"))
_value: str = field(metadata=config(field_name="value"))

def __init__(self, property: str, value: str):
self._property = property
Expand Down Expand Up @@ -219,7 +220,7 @@ def __str__(self):
class RemoveProperty:
"""A fileset change to remove a property from the fileset."""

_property: str = field(metadata=config(field_name='property'))
_property: str = field(metadata=config(field_name="property"))

def __init__(self, property: str):
self._property = property
Expand Down
1 change: 1 addition & 0 deletions clients/client-python/gravitino/api/metalake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import abstractmethod
from typing import Optional, Dict

Expand Down
21 changes: 13 additions & 8 deletions clients/client-python/gravitino/api/metalake_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from dataclasses import dataclass, field

from dataclasses_json import config
Expand All @@ -14,7 +15,7 @@ class MetalakeChange:
"""

@staticmethod
def rename(new_name: str) -> 'MetalakeChange.RenameMetalake':
def rename(new_name: str) -> "MetalakeChange.RenameMetalake":
"""Creates a new metalake change to rename the metalake.
Args:
Expand All @@ -26,7 +27,7 @@ def rename(new_name: str) -> 'MetalakeChange.RenameMetalake':
return MetalakeChange.RenameMetalake(new_name)

@staticmethod
def update_comment(new_comment: str) -> 'MetalakeChange.UpdateMetalakeComment':
def update_comment(new_comment: str) -> "MetalakeChange.UpdateMetalakeComment":
"""Creates a new metalake change to update the metalake comment.
Args:
Expand All @@ -38,7 +39,7 @@ def update_comment(new_comment: str) -> 'MetalakeChange.UpdateMetalakeComment':
return MetalakeChange.UpdateMetalakeComment(new_comment)

@staticmethod
def set_property(property: str, value: str) -> 'SetProperty':
def set_property(property: str, value: str) -> "SetProperty":
"""Creates a new metalake change to set a property and value pair for the metalake.
Args:
Expand All @@ -51,7 +52,7 @@ def set_property(property: str, value: str) -> 'SetProperty':
return MetalakeChange.SetProperty(property, value)

@staticmethod
def remove_property(property: str) -> 'RemoveProperty':
def remove_property(property: str) -> "RemoveProperty":
"""Creates a new metalake change to remove a property from the metalake.
Args:
Expand All @@ -65,7 +66,8 @@ def remove_property(property: str) -> 'RemoveProperty':
@dataclass(frozen=True)
class RenameMetalake:
"""A metalake change to rename the metalake."""
_new_name: str = field(metadata=config(field_name='new_name'))

_new_name: str = field(metadata=config(field_name="new_name"))

def new_name(self) -> str:
return self._new_name
Expand All @@ -76,7 +78,8 @@ def __str__(self):
@dataclass(frozen=True)
class UpdateMetalakeComment:
"""A metalake change to update the metalake comment"""
_new_comment: str = field(metadata=config(field_name='new_comment'))

_new_comment: str = field(metadata=config(field_name="new_comment"))

def new_comment(self) -> str:
return self._new_comment
Expand All @@ -87,8 +90,9 @@ def __str__(self):
@dataclass(frozen=True)
class SetProperty:
"""A metalake change to set a property and value pair for the metalake"""
_property: str = field(metadata=config(field_name='property'))
_value: str = field(metadata=config(field_name='value'))

_property: str = field(metadata=config(field_name="property"))
_value: str = field(metadata=config(field_name="value"))

def property(self) -> str:
return self._property
Expand All @@ -102,6 +106,7 @@ def __str__(self):
@dataclass(frozen=True)
class RemoveProperty:
"""A metalake change to remove a property from the metalake"""

_property: str

def property(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions clients/client-python/gravitino/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import abstractmethod
from typing import Optional, Dict

Expand Down
7 changes: 4 additions & 3 deletions clients/client-python/gravitino/api/schema_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import ABC
from dataclasses import field

Expand Down Expand Up @@ -39,8 +40,8 @@ def remove_property(property: str):
class SetProperty:
"""SchemaChange class to set the property and value pairs for the schema."""

_property: str = field(metadata=config(field_name='property'))
_value: str = field(metadata=config(field_name='value'))
_property: str = field(metadata=config(field_name="property"))
_value: str = field(metadata=config(field_name="value"))

def __init__(self, property: str, value: str):
self._property = property
Expand Down Expand Up @@ -97,7 +98,7 @@ def __str__(self):
class RemoveProperty:
"""SchemaChange class to remove a property from the schema."""

_property: str = field(metadata=config(field_name='property'))
_property: str = field(metadata=config(field_name="property"))

def __init__(self, property: str):
self._property = property
Expand Down
8 changes: 6 additions & 2 deletions clients/client-python/gravitino/api/supports_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2024 Datastrato Pvt Ltd.
This software is licensed under the Apache License version 2.
"""

from abc import ABC, abstractmethod
from typing import List, Dict

Expand All @@ -13,6 +14,7 @@

class NoSuchSchemaException(Exception):
"""Exception raised if the schema does not exist."""

pass


Expand Down Expand Up @@ -60,7 +62,9 @@ def schema_exists(self, ident: NameIdentifier) -> bool:
return False

@abstractmethod
def create_schema(self, ident: NameIdentifier, comment: str, properties: Dict[str, str]) -> Schema:
def create_schema(
self, ident: NameIdentifier, comment: str, properties: Dict[str, str]
) -> Schema:
"""Create a schema in the catalog.
Args:
Expand Down Expand Up @@ -110,7 +114,7 @@ def alter_schema(self, ident: NameIdentifier, *changes: SchemaChange) -> Schema:

@abstractmethod
def drop_schema(self, ident: NameIdentifier, cascade: bool) -> bool:
"""Drop a schema from the catalog. If cascade option is true, recursively
"""Drop a schema from the catalog. If cascade option is true, recursively
drop all objects within the schema.
Args:
Expand Down
Loading

0 comments on commit a88a361

Please sign in to comment.