Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add type annotations for rope.contrib.generate.create_generate() #641

Merged
merged 9 commits into from
Jan 5, 2023
33 changes: 28 additions & 5 deletions rope/contrib/generate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from rope.base import (
change,
codeanalyze,
Expand All @@ -10,13 +14,32 @@
)
from rope.refactor import functionutils, importutils, sourceutils, suites

if TYPE_CHECKING:
from typing import Literal, Optional

def create_generate(kind, project, resource, offset, goal_resource=None):
"""A factory for creating `Generate` objects
from rope.base.project import Project
from rope.base.resources import Resource

GenerateKind = Literal[
"variable",
"function",
"class",
"module",
"package",
]

`kind` can be 'variable', 'function', 'class', 'module' or
'package'.

def create_generate(
kind: GenerateKind,
project: Project,
resource: Resource,
offset: int,
goal_resource: Optional[Resource] = None,
):

"""A factory for creating `Generate` objects

Used in https://github.com/python-rope/ropemode but not in Rope itself.
"""
d = {
"class": GenerateClass,
Expand All @@ -25,7 +48,7 @@ def create_generate(kind, project, resource, offset, goal_resource=None):
"package": GeneratePackage,
"variable": GenerateVariable,
}
generate = d.get(kind)
generate = d[kind]
return generate(project, resource, offset, goal_resource=goal_resource)


Expand Down