Releases: isidentical/refactor
refactor v0.6.3
0.6.2
Major
Nothing new, compared to 0.6.0.
Other Changes
- Augmented and annotated assignments now counted towards definitions when analyzing the scope.
refactor.actions.InsertAfter
now preserves the final line state (e.g. if the anchor doesn't end with a newline, it also will produce code that won't be ending with a newline).getrefactor.com
is now available.
0.6.1
0.6.0
0.6.0
Major
This release adds experimental support for chained actions, a long awaited
feature. This would mean that each match()
can now return multiple actions (in
the form of an iterator), and they will be applied gradually.
import ast
from refactor import Rule, actions
from refactor.context import Representative, Scope
from typing import Iterator
class Usages(Representative):
context_providers = (Scope,)
def find(self, name: str, needle: ast.AST) -> Iterator[ast.AST]:
"""Iterate all possible usage sites of ``name``."""
for node in ast.walk(self.context.tree):
if isinstance(node, ast.Name) and node.id == name:
scope = self.context.scope.resolve(node)
if needle in scope.get_definitions(name):
yield node
class PropagateAndDelete(Rule):
context_providers = (Usages,)
def match(self, node: ast.Import) -> Iterator[actions.BaseAction]:
# Check if this is a single import with no alias.
assert isinstance(node, ast.Import)
assert len(node.names) == 1
[name] = node.names
assert name.asname is None
# Replace each usage of this module with its own __import__() call.
import_call = ast.Call(
func=ast.Name("__import__"),
args=[ast.Constant(name.name)],
keywords=[],
)
for usage in self.context.usages.find(name.name, node):
yield actions.Replace(usage, import_call)
# And finally remove the import itself
yield actions.Erase(node)
Other Changes
- Encoding is now preserved when using the
refactor.Session.run_file
API (which means if you use therefactor.Change.apply_diff
or using the-a
flag in the CLI, the generated source code will be encoded
with the original encoding before getting written to the file.) - Offset tracking has been changed to be at the encoded byte stream level (mirroring CPython behavior here). This fixes some unicode related problems (with actions like
refactor.actions.Replace
). refactor.context.ScopeInfo.get_definitions
now always returns a list, even
if it can't find any definitions.
0.5.0
0.5.0
Note: With this release, we also started improving our documentation. If you are interested in Refactor, check the new layout and tutorials out and let us know how you feel!
Major
This release includes the overhaul of our action system, and with the next releases we will start removing the old ones. A list
of changes regarding actions can be seen here:
refactor.core
no longer contains any actions (the deprecated aliases are still imported and exposed but all the new actions go intorefactor.actions
)Action
is now split into two, arefactor.actions.BaseAction
which is the base of all actions (useful for type hinting) and arefactor.actions.LazyReplace
(a replace action that builds the node lazily in itsbuild()
).ReplacementAction
is nowrefactor.actions.Replace
NewStatementAction
is nowrefactor.actions.LazyInsertAfter
TargetedNewStatementAction
is nowrefactor.actions.InsertAfter
For migrating your code base to the new style actions, we wrote a small tool (that we also used internally), examples/deprecated_aliases.py. Feel free to try it, and let us know if the transition was seamless.
Other Changes
- Added experimental Windows support, contributed by Hakan Celik
common.find_closest
now takesend_lineno
andend_col_offset
into account. It also ensures there is at least one target node.- Added
debug_mode
setting torefactor.context.Configuration
. - Added a command-line flag (
-d
/--enable-debug-mode
) to the default CLI runner to change session's configuration. - When unparsable source code is generated, the contents can be now seen if the debug mode is enabled.
- [Experimental] Added ability to partially recover floating comments (from preceding or succeeding lines) bound to statements.
- The context providers now can be accessed with attribute notation, e.g.
self.context.scope
instead ofself.context.metadata["scope]
. - If you access a built-in context provider (scope/ancestry) and it is not already imported, we auto-import it. So most common context providers are now ready to be used.
- Added
common.next_statement_of
.
0.5.0b0
What's Changed
Major
This release includes the overhaul of our action system, and with the next release we will be starting deprecating the old ones. A list
of changes:
refactor.core
no longer contains any actions (the deprecated aliases are still imported and exposed but all the new actions go intorefactor.actions
)Action
is now split into two, arefactor.actions.BaseAction
which is the base of all actions (useful for type hinting) and arefactor.actions.LazyReplace
(a replace action that builds the node lazily in itsbuild()
).ReplacementAction
is nowrefactor.actions.Replace
NewStatementAction
is nowrefactor.actions.LazyInsertAfter
TargetedNewStatementAction
is nowrefactor.actions.InsertAfter
For migrating your code base to the new style actions, we wrote a small tool (that we also used internally), examples/deprecated_aliases.py. Feel free to try it, and let us know if the transition was seamless.
Other Changes
- Added experimental Windows support, contributed by Hakan Celik
common.find_closest
now takesend_lineno
andend_col_offset
into account. It also ensures there is at least one target node.- Added
debug_mode
setting torefactor.context.Configuration
- Added a command-line flag (
-d
/--enable-debug-mode
) to the default CLI runner to change session's configuration. - When unparsable source code is generated, the contents can be now seen if the debug mode is enabled.
- [Experimental] Added ability to partially recover floating comments (from preceding or succeeding lines) bound to statements.
- The context providers now can be accessed with attribute notation, e.g.
self.context.scope
instead ofself.context.metadata["scope]
. - If you access a built-in context provider (scope/ancestry) and it is not already imported, we auto-import it. So most common context providers are now ready to be used.
0.4.4
What's Changed
- Use
pre-commit
as CI by @sobolevn in #29 - Fix when using keyword-only argument and default argument is not set by @hakancelikdev in #34
New Contributors
- @hakancelikdev made their first contribution in #34
Full Changelog: 0.4.3...0.4.4
0.4.3
0.4.2
What's Changed
- Fix passing --refactor-file, allow source directories by @gerrymanoim in #22
New Contributors
- @gerrymanoim made their first contribution in #22
Full Changelog: 0.4.1...0.4.2
0.4.1
What's Changed
- Preserve indented literal expressions by @isidentical in #19
Full Changelog: 0.4.0...0.4.1