Commit graph

1099 commits

Author SHA1 Message Date
Zsolt Dollenstein
9d611f9733
bump version to 0.3.23 2021-11-23 12:53:42 +00:00
Luke Petre
58b447d8f7
Fixes incorrectly missing annotations (#561)
Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2021-11-23 11:08:23 +00:00
Zsolt Dollenstein
3895925f15
bump version to 0.3.22 2021-11-22 14:06:19 +00:00
Luke Petre
d0f8fa97e2
Remove remnants of circleci (#552) 2021-11-19 15:04:02 +00:00
Giorgi Megreli
c48cc2101a
Move find_qualified_names_for in the Assignment class. (#557)
Move _NameUtil.find_qualified_name_for ... method inside Assignment classes.
2021-11-19 14:58:50 +00:00
Giorgi Megreli
9732f5ec82
Correct and simplify logic of recording assignments (#556) 2021-11-19 07:21:06 -05:00
Giorgi Megreli
ae8d0cda2f
Pin accesses to import alias node (#554)
* Add ImportAssignment class and record it from Scope

* Add overrides for LocalScope and ClassScope

* Clean scope_provider code and use ImportAssignment class in `unusued_imports` codemod

* Add missing types

* Fix fixit errors
2021-11-19 11:20:47 +00:00
Luke Petre
463f15e805
Test overwriting imports w/ global and nonlocal stmts (#553) 2021-11-18 17:08:19 -05:00
Zsolt Dollenstein
2cd43c4b78
disable setuptools local scheme when env var is set (#551) 2021-11-18 15:00:06 +00:00
Luke Petre
5c05001c2d
Publish test packages (#550) 2021-11-18 09:03:27 +00:00
Luke Petre
56386d7add
Stop parsing string annotations when no longer in a typing call (#546)
* Fix ScopeProvider when string type annotation is unparsable

* Handle nested function calls w/in type declarations

* Edit stack in place

* Add unparsed test to test_cast
2021-11-17 16:12:02 -05:00
Luke Petre
7db6ec5384
Swallow parsing errors in string annotations. (#548)
* Swallow parsing errors in string annotations.

This is the same behavior as cPython.

I've also rewritten the test that was relying on this exception to check where type parsing was happening

* Fix pyre error
2021-11-17 15:08:52 -05:00
Luke Petre
3e798726ec
Pass absolute path to codemod test (#549) 2021-11-17 14:37:53 -05:00
Luke Petre
70ff0f2182
Upgrade black to fix errors in 3.9.8 (#547) 2021-11-10 12:15:18 -05:00
Steven Troxler
182586cf5b
Support relative imports in ATAV qualifier handling (#538)
Based on diff review of https://github.com/Instagram/LibCST/pull/536,
I investigated relatvie import handling and realized that with minor
changes we can now handle them correctly.

Relative imports aren't likely in code coming from an automated
tool, but they could happen in hand-written stubs if anyone tries
to use this codemod tool to merge stubs with code.

Added a new test:
```
> python -m unittest libcst.codemod.visitors.tests.test_apply_type_annotations
.............................................
----------------------------------------------------------------------
Ran 45 tests in 2.195s

OK

```
2021-11-10 10:33:57 +00:00
Luke Petre
3dbcf5fed7
Don't gather metadata if the wrapper already contains it (#545)
Make sure that the MetadataWrapper to resolves the requested providers vs the existing metadata results and prevent a single provider from being invoked multiple times.
2021-11-08 15:34:24 -05:00
Steven Troxler
3ccfc4adc8
Fix typing errors on ATAV (#542) 2021-11-02 14:06:45 -05:00
Steven Troxler
a352d56970 Support use_future_annotations in ApplyTypeAnnotationsVisitor
Note: I'm pushing this because it works, but I actually want to
add annotation counting first and then modify the code so that we
only add the import if an annotation was actually included.
2021-10-30 11:57:19 -04:00
Steven Troxler
87625d02b6 Do not add imports if we added no type info in ATAV
In ApplyTypeAnnotationsVisitor, there are edge cases where we
might have changed the module imports even though we never wound
up applying any type annotations.

This will become even more common if we support adding
`from __future__ import annotations`, which I would like to do
soon.

To handle this, we can simply return the original tree from
`transform_module_impl` (discarding any changes from either
`self` or `AddImportsVisitor`) whenever there are no changes
in `self.annotation_counts`.

I updated the no-annotations-changed test to reflect this:
```
> python -m unittest libcst.codemod.visitors.tests.test_apply_type_annotations.TestApplyAnnotationsVisitor
...............................................
----------------------------------------------------------------------
Ran 47 tests in 2.312s

OK
```
2021-10-28 19:11:46 -04:00
Steven Troxler
3743c702dc
Use QualifiedNameProvider to handle stub types (#536)
The existing TypeCollector visitor logic attempted to
fold actual imports from stubs together with the module
we were annotating, and separately do nice things with the
names of types so that we could parse stubs written either
with various sorts of proper imports *or* stubs written
using bare fully-qualified type names (which isn't
actually legal python, but is easy to produce from automated
tools like `pyre infer`).

In this commit I simplify things in principle - meaning the
data flow is simpler, although the code is still similarly
complex - by using `QualifiedNameProvider` plus a fallback
to `get_full_name_for_node` to handle all cases via
fully-qualified names, so that the way a stub chooses to
lay out its imports is no longer relevant to how we will
understand it.

As a result, we can scrap a whole test suite where we
were understanding edge cases in the import handling, and
moreover one of the weird unsupported edge cases is now
well supported.

The tests got simpler because some edge cases no longer
matter (the whole imports test is no longer relevant),
and a couple of weird edge cases were fixed.

I ran tests with
```
python -m unittest libcst.codemod.visitors.tests.test_apply_type_annotations.TestApplyAnnotationsVisitor
```

I tried to make this change minimal in that I preserve the
existing data flow, so that it's easy to review. But it's worth
considering whether to follow up with a diff where we change
the TypeAnnotationCollector into a *transform* rather than a
*visitor*, because that would allow us to scrap quite a bit
of logic - all we would need to know is a couple of bits
of context from higher up in the tree and we could process
Names and Attributes without needing all this recursion.
2021-10-28 13:46:18 -04:00
Steven Troxler
1f169b8b8d
Count the information we add in ApplyTypeAnnotationsVisitor (#537)
Refactor ApplyTypeAnnotationsVisitor so that all annotation
information is added via a smart constructor method starting
with `_apply_annotation_to`. This makes it much easier to
skim the code and understand where annotations are actually
added with a simple forward search.

Then, add an AnnotationCounts dataclass and count up all the
annotations we add inside the transform. This should be helpful
for a few reasons:
- First, it just makes counting the annotations easier. Prior
  to this change, we would have to run some separate command
  to count annotations before and after a codemod, which is
  not as convenient as doing it directly, and would also fail
  to account for cases where we changed an annotation.
- Second, I want to be able to avoid altering the import
  statements in cases where we never actually made any changes.
  Having annotation counts will help us do this - we can just
  return the original tree (without import changes) in that
  situtation.

```
> python -m unittest libcst.codemod.visitors.tests.test_apply_type_annotations.TestApplyAnnotationsVisitor
................................................
----------------------------------------------------------------------
Ran 48 tests in 1.773s

OK
```

(
2021-10-28 12:46:57 -04:00
Steven Troxler
14eff6aaf5
Remove a dead method in ApplyTypeAnnotationsVisitor (#533)
I'm not really sure how the method got there, but it was calling
itself recursively... fortunately, it was also overwritten by
an identically named method so it was actually impossible to access.
2021-10-27 11:08:38 -04:00
Zac Hatfield-Dodds
1e0d4841bf
Fix m.OneOf() docs (#529)
Closes #345.
2021-10-20 22:25:59 -07:00
MapleCCC
13485d3c2f
Fix documentation typos (#527) 2021-10-01 16:40:47 +01:00
John Reese
71b8002cca
Add --indent-string option to libcst.tool print (#525)
Allows passing a custom indent string, like ".   ", for easier visual
parsing of the resulting tree.
2021-09-28 09:47:12 -07:00
Zsolt Dollenstein
39607edb2d
Bump version to 0.3.21 (#524) 2021-09-21 19:39:34 +01:00
Luke Petre
a20d43e7e6
Rewrite the MatchIfTrue type to be generic on _MatchIfTrueT (#512) 2021-09-21 09:53:17 +01:00
Rodrigo Zhou
683731b1e1
Fix pyre command for type inference provider (#523)
* Fix pyre command for type inference provider

* fix integration test

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2021-09-16 17:59:24 +01:00
Steven Troxler
fe0dc1b8a4
Improve the tests of annotating async / decorator functions (#518)
The existing two tests didn't make it clear what exactly we wanted
to verify, which is two things:
- that we can successfully annotate async functions with decorators
- that it doesn't matter whether or not the async and decorator
  information is part of the stubs - we need it to be permissible
  because a "real" stubs file would have this, but stubs generated
  by tools like pyre infer shouldn't need to care, they only
  really need to care about types
2021-09-07 12:23:37 +01:00
Steven Troxler
69156c7775
Bump pyre-check to latest version (#516)
The current version of the pyre query api requires a running
watchman server, which has to be started independently.
2021-09-07 12:08:46 +01:00
Zsolt Dollenstein
45c0d96c06
master -> main (#521) 2021-09-07 11:59:38 +01:00
Steven Troxler
2e6fb89fcc
Organize the _apply_type_annotations visitor tests (#517) 2021-08-27 09:06:35 +01:00
Steven Troxler
96a0b53bfe
Extract common utilities in ApplyTypeAnnotationsVisitor tests (#514)
All of our tests follow one of two patterns: either populate
a context and transform using the default behavior, or test
when setting flags in either the context population and transform
steps (and verify that the behavior is the same in both cases).

So, extract these two patterns into helper functions. This improves
readability of the existing code a bit, and will be even more helpful
if we split apart the monster test `test_annotate_functions` (which
I would like to do soon - the list of test cases is so big that it's
hard to jump to the relevant section when trying to verify behaviors).
2021-08-26 08:31:38 -04:00
Steven Troxler
5e1e3fe970
The ufmt tool combines usort and black with a consistent wrapper, (#515)
which ensures we won't have inconsistent black-vs-isort errors
going forward. We can always format by running `ufmt format .`
at the root, and check with `ufmt check .` in our CI actions.
2021-08-25 20:39:29 -04:00
Luke Petre
5928f6ad81
Windows CI support (#505)
* Support windows testing in github actions

* Fix path handling in windows
2021-08-12 15:41:23 -04:00
Luke Petre
695f844c6d
Split up some larger CI/tox steps into separate jobs (#511) 2021-08-12 15:18:19 -04:00
Luke Petre
1cceed6df8
Adding python3.9 to the CI (#506) 2021-08-11 17:16:58 +01:00
Luke Petre
398d14e91b
Switch code coverage from tox => gh actions (#510) 2021-08-11 11:41:18 -04:00
Luke Petre
87f0becd01
Change codegen to treat typing.Union[Foo, NoneType] and typing.Optional[Foo] as the same (#508) 2021-08-11 09:48:30 -04:00
Zsolt Dollenstein
03b3933af6
remove circleci config and update readme (#509) 2021-08-11 08:50:18 -04:00
Luke Petre
1c3a27bbd5
Add Github Actions and derive version from git tags (#471)
* Use setuptools-scm to derive the current version from git metadata

* Add Github Action equivalent to the current circleci tasks

* Run pyre integration test in GH action / tox
2021-08-10 15:01:16 +01:00
Zsolt Dollenstein
a1282f27f6
bump version to 0.3.20 (#504) 2021-08-10 10:37:18 +01:00
Steven Troxler
3009ec9e36
Add tests verifying TypeAnnotationsVisitor current behaviors (#502)
* Add tests to verify that LibCST handles string annotations.

This is an important property for certain use cases, so it
makes sense to verify it in tests so that we can safely
depend on it.

At present, the reason we want to be able to rely on this is:
- at the moment, imports added by infer can make pysa traces
  hard to understand, because the line numbers are off
- if we add the ability to use fully-qualified string annotations
  for the stubs from infer, then we can do so without adding
  any import lines and pyre will understand the types.

* ApplyTypeAnnotations: add unit test of how import statments are merged

Add a unit test illustrating how the codemod handles various cases
of import statments in the stub file.

Explicitly call out each of the unsupported patterns:
- bare imports (we probably should support this)
- relative imports (we probably should support this)
 star imports (we probably don't want to support this)

* Add .python-version to .gitignore

This will be helpful for anyone using pyenv (I accidentally committed
my python version file in a draft branch).
2021-08-06 08:55:47 -07:00
Zach Hammer
c7f2f577ac
Docs: fix typo (#492)
Caught this while quoting your docs in a blog post, which I'll share once published :)
2021-07-28 09:53:50 +01:00
Steven Troxler
19d9a0761f
erify TypeAnnotationsVisitor partial-stub behavior, fix bug (#499)
**What?**

I added tests of how TypeAnnotationsVisitor reacts to
- stubs that ignore decorators and the async keyword
- stubs that lack parameter and/or return annotations where
  a callable has them.

I discovered one case where TypeAnnotationsVisitor didn't do what
I want it to do: if we're missing a return annotation in the stubs
but have one in our function and `overwrite_existing_annotations` is
set, we'll actually strip out the existing annotation. So I tweaked
the logic to only use the annotation from the stub if it is nonmissing.

**Why?**

I'm working on a project (pyre infer) that depends on
TypeAnnotationsVisitor, and a problem we've run into is that we don't
yet have perfect logic for resolving imports in a way that's
sufficiently sensitive to local context to be good for codemods - for
example, we're not yet sure how to reliably diff against the existing
imports for aliased imports (when bar imports foo.Foo and then baz
includes `from bar import Foo`) and relative imports.

Because we try to give full type annotations on parameters, even where
there is a preexisting annotation, this can lead to a lot of noise
where diffs fail because of imports changing due to the stub even
though there was no actual change to the annotations.

In the short term, the easiest solution is to simply omit given
annotations from the stubs we generate. But that means we have to
be sure TypeAnnotations

**Test Plan**

```
> python -m unittest libcst.codemod.visitors.tests.test_apply_type_annotations
...........................................
----------------------------------------------------------------------
Ran 43 tests in 1.548s

OK
```
2021-07-28 09:52:59 +01:00
Jos Verlinde
8c19f1ca83
fix codemodding on windows (#495)
* pass empty environment
fixes: https://github.com/Instagram/LibCST/issues/494

* remove empty env to support windows.
2021-07-05 12:27:41 +01:00
Zsolt Dollenstein
e0dd6016a5
bump version to 0.3.19 (#484)
* bump version to 0.3.19

* Update CHANGELOG.md

Co-authored-by: Jimmy Lai <yurinai@gmail.com>

Co-authored-by: Jimmy Lai <yurinai@gmail.com>
2021-05-13 15:38:04 +01:00
Zsolt Dollenstein
4d2ccc54b2
tie accesses from string annotation to the string node (#483) 2021-05-12 14:50:15 +01:00
Bruno Alla
d1606b7077
Fix formatting for link to QualifiedName class in docs (#480) 2021-04-28 10:19:54 +01:00
Zsolt Dollenstein
3b21fa9da9
Return more specific QNames for assignments (#477)
When `scope.get_qualified_names_for()` is called with a node that's an `Assignment`, return the qualified name for that node instead of all the assignments for the same name.
2021-04-19 14:41:12 +01:00