* generate Attribute nodes when applying type annotations
The old version generated an incorrect CST which
happened to work as long as you didn't do further processing.
* add a test
* Keep old exception messages (avoid breaking-changes for users relying on exception messages)
* Move ``get_expected_str`` out of _exceptions.py, where it does not belong, to its own file in _parser/_parsing_check.py
This PR:
1. marks the `libcst.native` module as free-threading-compatible
2. replaces the use of ProcessPoolExecutor with ThreadPoolExecutor if free-threaded CPython is detected at runtime
Instead of sharing instances of a Codemod across many files, this PR allows passing in a Codemod class to `parallel_exec_transform_with_prettyprint` which will then instantiate the Codemod for each file. `tool._codemod_impl` now starts using this API.
The old behavior is deprecated, because sharing codemod instances across files is a surprising behavior, and causes hard-to-diagnose bugs when a Codemod keeps track of its state via instance variables.
Instead of relying on `multiprocessing.Pool`, this PR replaces the implementation of `parallel_exec_transform_with_prettyprint` with `concurrent.futures.ProcessPoolExecutor`
When renaming `a.b` -> `c.d`, in imports like `import a.b as x` the as_name wasn't correctly removed even though references to `x` were renamed to `c.d`.
This PR makes the codemod remove the `x` asname in these cases.
* Add is_property check
Skip properties to prevent exceptions
* Delayed initialization of matchers
To support multiprocessing on Windows/macOS
Issue #1181
* Add a test for matcher decorators with multiprocessing
* Clean warnings for each file in comemod cli
* Fix ZeroDivisionError: float division by zero
When codemodding too fast
* Recreate CodemodContext for each file
Keep only context.metadata_manager
Remove wrapper from context defaults on each file
#453 fixed scratch leaking between files by setting it to empty, but that drops all the scratch space that was set up before the codemod runs (e.g. in the transformer's constructor)
This PR improves the fix by preserving the initial scratch.
Summary:
This PR removes the `typing_extensions` and `typing_inspect` dependencies as we can now rely on the built-in `typing` module since Python 3.9.
Test Plan:
existing tests
* Update test_fix_pyre_directives.py
refactor with fstring to format string to make code more Pythonic.
* Update test_fix_pyre_directives.py
refactor with fstring to format string to make code more Pythonic.
* Update test_fix_pyre_directives.py
refactor with fstring to format string to make code more Pythonic.
* Update test_fix_pyre_directives.py
refactor with fstring to format string to make code more Pythonic.
* Update test_fix_pyre_directives.py
refactor with chain constant value assignment to make code more Pythonic
* Update test_fix_pyre_directives.py
refactor with chain constant value assignment to make code more Pythonic
* AddImportsVisitor will now only add at the top of module
- Also added new tests to cover these cases
* Fixed an issue with from imports
* Added a couple tests for AddImportsVisitor
* Refactoring of GatherImportsVisitor
* Refactors, typos and typing changes
* Fix type of evaluated_value on string
This can return bytes if the string is a bytestring, e.g.:
In [1]: import libcst as cst
In [2]: cst.parse_expression('b"foo"').evaluated_value
Out[2]: b'foo'
* Fix type errors from changed signature
* Fixes an issue where ApplyTypeAnnotationsVisitor would crash on code
like `SomeClass.some_attribute = 42` with a "Name is not a valid
identifier" error message.
* Changes the above-mentioned error message to include the bad name in
the message, for easier debugging.
* Adds tests for all valid assignment targets, as described here:
https://libcst.readthedocs.io/en/latest/nodes.html#libcst.BaseAssignTargetExpression.
* Simplify command specifier parsing
* Allow running codemods without configuring in YAML
This enables codemodding things by just plonking a CodemodCommand class
into any old importable module and running
`python -m libcst.tool codemod -x some_module.SomeClass ...`
Moves PEP 621 metadata from `setup.py` and `requirements*.txt` into the
`[project]` table of `pyproject.toml`. This enables using hatch as a
task runner for the project, where previously one would need to remember
a bunch of different commands, or repeatedly consult the readme's
developer guide to find all of the relevant commands.
This creates the following hatch commands:
- docs
- fixtures
- format
- lint
- test
- typecheck
It also updates all of the github actions workflows to use the
appropriate hatch commands, and the readme's developer guide, so that
there is only one source of truth for what constitutes running tests.
The "test" workflows now drop the matrix distinction between "pure" or
"native", and run tests in both modes from a single build.
ghstack-source-id: 8834da7825
Pull Request resolved: https://github.com/Instagram/LibCST/pull/893
* Enumeration members are singletons. Copying on them would be no-op
* Avoid generating unnecessary `pass` statement
* Several trivial refactor
* Avoid building unnecessary intermediate lists, which are mere slight waste of time and space
* Remove unused import, an overlook from commit 8e6bf9e9
* `collections.abc.Mapping.get()` defaults to return `None` when key doesn't exist
* Just use unittest's `assertRaises` to specify expected exception types, instead of catching every possible `Exception`s, which could suppress legitimate errors and hide bugs
* We know for sure that the body of `CSTTypedTransformerFunctions` won't be empty, so don't bother with complex formal completeness
* When codemod, specify the black formatter to use the same target Python version we use
* Fix the `test_codemod_formatter_error_input` unit test
* Remove an unused import in `test_codemod_cli` module
Correct the renamed import from structure when renaming last imported name from a module.
Given
from a.b import qux
print(qux)
And providing old_name="a.b.qux" and new_name="a:b.qux" I expect the
following output (as described int the command description):
from a import b
print(b.qux)
But what I get is:
from a import b.qux
print(b.qux)
It pulls the old name up into the new one.
The provided test is the important part but I've attempted a fix too. I
suspect there is a better one and that the special casing of the "this
is that last name" situation shouldn't be needed. For instance there is
import removing code in leave_Module and renaming the first of many
names (as opposed to the last) happily adds a correct import line.
I didn't manage to grok the code and all the concepts it requires to
provide a better fix though.
This leaves the alias adjustments to the existing code and just does the
module renaming the int he special casing block.
I don't know why scheduling removal of the updated node is required, it makes
the tests pass though.
* Qualify imported symbols when the dequalified form would cause a conflict.
Adds a preliminary pass that scans the stub file for all imported
symbols, and collects the ones that cannot be safely dequalified.
Fixes#673
* review fixes
* handle symbol conflicts between the stub and the main file
* fix type errors