FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Finish adding type annotations to admin code · 7dracoder/python-spanner-orm@58543a9 · GitHub

Commit 58543a9

Browse files
committed
Finish adding type annotations to admin code
1 parent 6b2e8a3 commit 58543a9

7 files changed

Lines changed: 145 additions & 103 deletions

File tree

‎spanner_orm/admin/migration_executor.py‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
"""Handles execution of migrations."""
16+
17+
from __future__ import annotations
18+
1619
import datetime
1720
import logging
21+
from typing import Any, Dict, Optional
1822

1923
from spanner_orm import api
2024
from spanner_orm import error
@@ -24,34 +28,37 @@
2428
from spanner_orm.admin import migration_status
2529
from spanner_orm.admin import update
2630

31+
from google.auth import credentials as auth_credentials
32+
2733
_logger = logging.getLogger(__name__)
2834

2935

30-
class MigrationExecutor(object):
36+
class MigrationExecutor:
3137
"""Handles execution of migrations."""
3238

3339
def __init__(self,
34-
instance,
35-
database,
36-
project=None,
37-
credentials=None,
38-
basedir=None):
40+
instance: str,
41+
database: str,
42+
project: Optional[str] = None,
43+
credentials: Optional[auth_credentials.Credentials] = None,
44+
basedir: Optional[str] = None):
3945
self._manager = migration_manager.MigrationManager(basedir)
4046
self._migration_status_map = None
4147
self._instance = instance
4248
self._database = database
4349
self._project = project
4450
self._credentials = credentials
4551

46-
def migrated(self, migration_id):
52+
def migrated(self, migration_id: str) -> bool:
4753
if migration_id is None:
4854
return True
4955
return self._migration_status().get(migration_id, False)
5056

51-
def migrations(self):
57+
# TODO(dbrandao): make a Migration object so this is no longer Any
58+
def migrations(self) -> Any:
5259
return self._manager.migrations
5360

54-
def migrate(self, target_migration=None):
61+
def migrate(self, target_migration: Optional[str] = None) -> None:
5562
"""Executes unmigrated migrations on the curent database.
5663
5764
Note: SpannerApi and SpannerAdminApi connections are modified as a result
@@ -79,7 +86,7 @@ def migrate(self, target_migration=None):
7986
self._update_status(migration.migration_id, True)
8087
self._hangup()
8188

82-
def rollback(self, target_migration):
89+
def rollback(self, target_migration: str) -> None:
8390
"""Rolls back migrated migrations on the curent database.
8491
8592
Note: SpannerApi and SpannerAdminApi connections are modified as a result
@@ -110,7 +117,7 @@ def rollback(self, target_migration):
110117
self._update_status(migration.migration_id, False)
111118
self._hangup()
112119

113-
def _connect(self):
120+
def _connect(self) -> None:
114121
admin_api.SpannerAdminApi.connect(
115122
self._instance,
116123
self._database,
@@ -122,11 +129,12 @@ def _connect(self):
122129
project=self._project,
123130
credentials=self._credentials)
124131

125-
def _hangup(self):
132+
def _hangup(self) -> None:
126133
admin_api.SpannerAdminApi.hangup()
127134
api.SpannerApi.hangup()
128135

129-
def _filter_migrations(self, migrations, migrated, last_migration):
136+
def _filter_migrations(self, migrations: Any, migrated: bool,
137+
last_migration: Optional[str]) -> Any:
130138
"""Filters the list of migrations according to the desired conditions.
131139
132140
Args:
@@ -153,7 +161,7 @@ def _filter_migrations(self, migrations, migrated, last_migration):
153161
last_migration))
154162
return filtered
155163

156-
def _migration_status(self):
164+
def _migration_status(self) -> Dict[str, bool]:
157165
"""Gathers from Spanner which migrations have been executed."""
158166
if self._migration_status_map is None:
159167
model_from_db = metadata.SpannerMetadata.model(
@@ -167,7 +175,7 @@ def _migration_status(self):
167175

168176
return self._migration_status_map
169177

170-
def _update_status(self, migration_id, new_status):
178+
def _update_status(self, migration_id: str, new_status: bool) -> None:
171179
"""Updates migration status in the database for the given migration."""
172180
new_model = migration_status.MigrationStatus({
173181
'id': migration_id,
@@ -178,7 +186,7 @@ def _update_status(self, migration_id, new_status):
178186
None, [new_model], force_write=True)
179187
self._migration_status()[migration_id] = new_status
180188

181-
def _validate_migrations(self):
189+
def _validate_migrations(self) -> None:
182190
"""Validates the migration status of all migrations makes sense."""
183191
migrations = self.migrations()
184192
if not migrations:

‎spanner_orm/admin/migration_manager.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,33 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
"""Handles reading and writing of migration files."""
16+
17+
from __future__ import annotations
18+
19+
1620
import datetime
1721
import importlib
1822
import os
1923
import re
2024
import string
25+
from typing import Any, Iterable, List, Optional
2126
import uuid
2227

2328
from spanner_orm import error
2429

2530

26-
class MigrationManager(object):
31+
class MigrationManager:
2732
"""Handles reading and writing of migration files."""
2833
DEFAULT_DIRECTORY = 'migrations'
2934

30-
def __init__(self, basedir=None):
35+
def __init__(self, basedir: Optional[str] = None):
3136
self.basedir = basedir or self.DEFAULT_DIRECTORY
3237
self._migrations = None
3338

3439
if not os.path.exists(self.basedir):
3540
os.makedirs(self.basedir)
3641

37-
def generate(self, migration_name):
42+
def generate(self, migration_name: str) -> str:
3843
"""Creates a new migration that is the last migration to be executed."""
3944
migration_id = uuid.uuid4().hex[-12:]
4045
prev_id = self.migrations[-1].migration_id if self.migrations else None
@@ -58,14 +63,14 @@ def generate(self, migration_name):
5863
return filepath
5964

6065
@property
61-
def migrations(self):
66+
def migrations(self) -> Any:
6267
"""Loads and orders all migrations in the base dir."""
6368
if self._migrations is None:
6469
unordered_migrations = self._all_migrations()
6570
self._migrations = self._order_migrations(unordered_migrations)
6671
return self._migrations
6772

68-
def _migration_from_file(self, filename):
73+
def _migration_from_file(self, filename: str) -> Any:
6974
"""Loads a single migration from the given filename in the base dir."""
7075
module_name = re.sub(r'\W', '_', filename)
7176
path = os.path.join(self.basedir, filename)
@@ -76,7 +81,7 @@ def _migration_from_file(self, filename):
7681
raise error.SpannerError('{} has no migration id'.format(path))
7782
return module
7883

79-
def _all_migrations(self):
84+
def _all_migrations(self) -> List[Any]:
8085
"""Loads all migrations from the base dir."""
8186
migrations = []
8287
for filename in os.listdir(self.basedir):
@@ -85,7 +90,7 @@ def _all_migrations(self):
8590
migrations.append(self._migration_from_file(filename))
8691
return migrations
8792

88-
def _order_migrations(self, migrations):
93+
def _order_migrations(self, migrations: Iterable[Any]) -> List[Any]:
8994
"""Returns list of migrations in the order they have to be applied."""
9095
if not migrations:
9196
return []

‎spanner_orm/admin/scripts.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
"""Entry point for spanner_orm scripts."""
16+
17+
from __future__ import annotations
18+
1619
import argparse
20+
from typing import Any
21+
1722
from spanner_orm.admin import migration_manager
1823

1924

20-
def generate(args):
25+
def generate(args: Any) -> None:
2126
manager = migration_manager.MigrationManager(args.directory)
2227
manager.generate(args.name)
2328

2429

25-
def main(as_module=False):
30+
def main(as_module: bool = False) -> None:
2631
prog = 'spanner-orm' if as_module else None
2732
parser = argparse.ArgumentParser(prog=prog)
2833
subparsers = parser.add_subparsers(

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL