/
gather.py
1273 lines (1071 loc) · 45 KB
/
gather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tools for gathering stats about typeshed packages."""
import ast
import asyncio
import json
import os
import re
import sys
import urllib.parse
from collections import Counter
from collections.abc import Collection, Container, Iterable, Iterator, Mapping, Sequence
from contextlib import AsyncExitStack, contextmanager
from enum import Enum
from functools import lru_cache, partial
from itertools import chain
from operator import attrgetter
from pathlib import Path
from typing import (
Annotated,
Any,
Literal,
NamedTuple,
NewType,
TypeAlias,
TypeGuard,
TypeVar,
final,
)
import aiohttp
import attrs
from packaging.specifiers import SpecifierSet
from packaging.version import Version
from pathspec import PathSpec
from pathspec.patterns.gitwildmatch import GitWildMatchPattern
if sys.version_info < (3, 10):
raise ImportError("Python 3.10+ is required!")
if sys.version_info >= (3, 11):
import tomllib # pragma: >=3.11 cover
else:
import tomli as tomllib # pragma: <3.11 cover
__all__ = [
"AnnotationStats",
"FileInfo",
"PackageInfo",
"PackageName",
"PackageStatus",
"PyrightSetting",
"StubtestSettings",
"StubtestStrictness",
"UploadStatus",
"gather_annotation_stats_on_file",
"gather_annotation_stats_on_package",
"gather_stats_on_file",
"gather_stats_on_multiple_packages",
"gather_stats_on_package",
"get_number_of_lines_of_file",
"get_package_extra_description",
"get_package_size",
"get_package_status",
"get_pyright_setting_for_package",
"get_pyright_setting_for_path",
"get_stub_distribution_name",
"get_stubtest_allowlist_length",
"get_stubtest_platforms",
"get_stubtest_settings",
"get_stubtest_strictness",
"get_upload_status",
"tmpdir_typeshed",
]
PackageName: TypeAlias = str
_AbsolutePath = NewType("_AbsolutePath", Path)
_PathRelativeToTypeshed: TypeAlias = Path
_NiceReprEnumSelf = TypeVar("_NiceReprEnumSelf", bound="_NiceReprEnum")
class _NiceReprEnum(Enum):
"""Base class for several public-API enums in this package."""
def __new__(cls: type[_NiceReprEnumSelf], doc: str) -> _NiceReprEnumSelf:
assert isinstance(doc, str)
member = object.__new__(cls)
member._value_ = member.__doc__ = doc
return member
def __repr__(self) -> str:
return f"{self.__class__.__name__}.{self.name}"
@property
def formatted_name(self) -> str:
return " ".join(self.name.split("_")).lower()
@attrs.define
class _SingleAnnotationAnalysis:
Any_in_annotation: bool = False
Incomplete_in_annotation: bool = False
class _SingleAnnotationAnalyzer(ast.NodeVisitor):
def __init__(self) -> None:
self.analysis = _SingleAnnotationAnalysis()
def __repr__(self) -> str:
return f"{self.__class__.__name__}(analysis={self.analysis})"
def visit_Name(self, node: ast.Name) -> None:
match node.id:
case "Any":
self.analysis.Any_in_annotation = True
case "Incomplete":
self.analysis.Incomplete_in_annotation = True
case _:
pass
self.generic_visit(node)
def visit_Attribute(self, node: ast.Attribute) -> None:
value = node.value
if isinstance(value, ast.Name):
match f"{value.id}.{node.attr}":
case "typing.Any" | "typing_extensions.Any":
self.analysis.Any_in_annotation = True
case "_typeshed.Incomplete":
self.analysis.Incomplete_in_annotation = True
case _:
pass
self.generic_visit(node)
def _analyse_annotation(annotation: ast.AST) -> _SingleAnnotationAnalysis:
analyser = _SingleAnnotationAnalyzer()
analyser.visit(annotation)
return analyser.analysis
@final
@attrs.define
class AnnotationStats:
"""Stats on the annotations for a source file or a directory of source files."""
annotated_parameters: int = 0
unannotated_parameters: int = 0
annotated_returns: int = 0
unannotated_returns: int = 0
explicit_Incomplete_parameters: int = 0
explicit_Incomplete_returns: int = 0
explicit_Any_parameters: int = 0
explicit_Any_returns: int = 0
annotated_variables: int = 0
explicit_Any_variables: int = 0
explicit_Incomplete_variables: int = 0
classdefs: int = 0
classdefs_with_Any: int = 0
classdefs_with_Incomplete: int = 0
def _node_matches_name(node: ast.expr, name: str, from_: Container[str]) -> bool:
"""Return True if `node` represents `name` from one of the modules in `from_`.
```pycon
>>> _is_TypeAlias = partial(
... _node_matches_name, name="TypeAlias", from_={"typing", "typing_extensions"}
... )
>>> get_annotation_node = lambda source: ast.parse(source).body[0].annotation
>>> _is_TypeAlias(get_annotation_node("foo: TypeAlias = int"))
True
>>> _is_TypeAlias(get_annotation_node("foo: typing.TypeAlias = int"))
True
>>> _is_TypeAlias(get_annotation_node("foo: typing_extensions.TypeAlias = int"))
True
>>> _is_TypeAlias(get_annotation_node("foo: int"))
False
>>> _is_TypeAlias(get_annotation_node("foo: Final = 5"))
False
```
"""
match node:
case ast.Name(id):
return id == name
case ast.Attribute(ast.Name(module), id):
return id == name and module in from_
case _:
return False
_is_staticmethod = partial(_node_matches_name, name="staticmethod", from_={"builtins"})
_is_TypeAlias = partial(
_node_matches_name, name="TypeAlias", from_={"typing", "typing_extensions"}
)
class _AnnotationStatsCollector(ast.NodeVisitor):
"""AST Visitor for collecting stats on a single stub file."""
def __init__(self) -> None:
self.stats = AnnotationStats()
self._class_nesting = 0
def __repr__(self) -> str:
return f"{self.__class__.__name__}(stats={self.stats})"
@property
def in_class(self) -> bool:
"""Return `True` if we're currently visiting a class definition."""
return bool(self._class_nesting)
def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.stats.classdefs += 1
base_analyses = [_analyse_annotation(base) for base in node.bases]
self.stats.classdefs_with_Any += any(
analysis.Any_in_annotation for analysis in base_analyses
)
self.stats.classdefs_with_Incomplete += any(
analysis.Incomplete_in_annotation for analysis in base_analyses
)
self._class_nesting += 1
self.generic_visit(node)
self._class_nesting -= 1
def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
self.generic_visit(node)
annotation = node.annotation
if _is_TypeAlias(annotation):
return
self.stats.annotated_variables += 1
analysis = _analyse_annotation(annotation)
self.stats.explicit_Any_variables += analysis.Any_in_annotation
self.stats.explicit_Incomplete_variables += analysis.Incomplete_in_annotation
def _visit_arg(self, node: ast.arg) -> None:
annotation = node.annotation
if annotation is None:
self.stats.unannotated_parameters += 1
else:
self.stats.annotated_parameters += 1
analysis = _analyse_annotation(annotation)
self.stats.explicit_Any_parameters += analysis.Any_in_annotation
self.stats.explicit_Incomplete_parameters += (
analysis.Incomplete_in_annotation
)
def _visit_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:
self.generic_visit(node)
returns = node.returns
if returns is None:
self.stats.unannotated_returns += 1
else:
self.stats.annotated_returns += 1
analysis = _analyse_annotation(returns)
self.stats.explicit_Any_returns += analysis.Any_in_annotation
self.stats.explicit_Incomplete_returns += analysis.Incomplete_in_annotation
args = node.args
for i, arg_node in enumerate(chain(args.posonlyargs, args.args)):
if (
i == 0
and self.in_class
and not any(
_is_staticmethod(decorator) for decorator in node.decorator_list
)
):
# We don't want self/cls/metacls/mcls arguments to count towards the statistics
# Whatever they're called, they can easily be inferred
continue
self._visit_arg(arg_node)
for arg_node in args.kwonlyargs:
self._visit_arg(arg_node)
for arg_node in filter(None, (args.vararg, args.kwarg)):
self._visit_arg(arg_node)
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
self._visit_function(node)
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
self._visit_function(node)
def gather_annotation_stats_on_file(path: Path | str) -> AnnotationStats:
"""Gather annotation stats on a single typeshed stub file.
Parameters:
path: The location of the file to be analysed.
Returns:
An [`AnnotationStats`](./#AnnotationStats) object
containing data about the annotations in the file.
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, gather_annotation_stats_on_file
>>> with tmpdir_typeshed() as typeshed:
... stats_on_functools = gather_annotation_stats_on_file(typeshed / "stdlib" / "functools.pyi")
...
>>> type(stats_on_functools)
<class 'typeshed_stats.gather.AnnotationStats'>
>>> stats_on_functools.unannotated_parameters
0
"""
visitor = _AnnotationStatsCollector()
with open(path, encoding="utf-8") as file:
visitor.visit(ast.parse(file.read()))
return visitor.stats
@lru_cache
def _get_package_directory(package_name: PackageName, typeshed_dir: Path | str) -> Path:
if package_name == "stdlib":
return Path(typeshed_dir, "stdlib")
return Path(typeshed_dir, "stubs", package_name)
def gather_annotation_stats_on_package(
package_name: PackageName, *, typeshed_dir: Path | str
) -> AnnotationStats:
"""Aggregate annotation stats on a typeshed stubs package.
Parameters:
package_name: The name of the stubs package to analyze.
typeshed_dir: A path pointing to the location of a typeshed directory
in which to find the stubs package source.
Returns:
An [`AnnotationStats`](./#AnnotationStats) object
containing data about the annotations in the package.
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, gather_annotation_stats_on_package
>>> with tmpdir_typeshed() as typeshed:
... mypy_extensions_stats = gather_annotation_stats_on_package("mypy-extensions", typeshed_dir=typeshed)
...
>>> type(mypy_extensions_stats)
<class 'typeshed_stats.gather.AnnotationStats'>
>>> mypy_extensions_stats.unannotated_parameters
0
"""
combined: Counter[str] = Counter()
annot_stats_fields = AnnotationStats.__annotations__
for path in _get_package_directory(package_name, typeshed_dir).rglob("*.pyi"):
file_stats = gather_annotation_stats_on_file(path)
for field in annot_stats_fields:
combined[field] += getattr(file_stats, field)
return AnnotationStats(**combined)
@lru_cache
def _get_package_metadata(
package_name: PackageName, typeshed_dir: Path | str
) -> Mapping[str, Any]:
package_directory = _get_package_directory(package_name, typeshed_dir)
with open(package_directory / "METADATA.toml", "rb") as f:
return tomllib.load(f)
def get_package_extra_description(
package_name: PackageName, *, typeshed_dir: Path | str
) -> str | None:
"""Get the "extra description" of the package given in the `METADATA.toml` file.
Each typeshed package comes with a `METADATA.toml` file,
containing various useful pieces of information about the package.
Parameters:
package_name: The name of the package to find the extra description for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the description.
Returns:
The "extra description" of the package given in the `METADATA.toml` file,
if one is given, else [None][].
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_package_extra_description
>>> with tmpdir_typeshed() as typeshed:
... stdlib_description = get_package_extra_description("stdlib", typeshed_dir=typeshed)
... protobuf_description = get_package_extra_description("protobuf", typeshed_dir=typeshed)
>>> stdlib_description is None
True
>>> isinstance(protobuf_description, str)
True
"""
if package_name == "stdlib":
return None
return _get_package_metadata(package_name, typeshed_dir).get("extra_description")
class StubtestStrictness(_NiceReprEnum):
"""Enumeration of the various possible settings typeshed uses for [stubtest][] in CI."""
SKIPPED = "Stubtest is skipped in typeshed's CI for this package."
MISSING_STUBS_IGNORED = (
"The `--ignore-missing-stub` stubtest setting is used in typeshed's CI."
)
ERROR_ON_MISSING_STUB = (
"Objects missing from the stub cause stubtest to emit an error "
"in typeshed's CI."
)
@lru_cache
def _get_stubtest_config(
package_name: PackageName, typeshed_dir: Path | str
) -> Mapping[str, object]:
metadata = _get_package_metadata(package_name, typeshed_dir)
config: object = metadata.get("tool", {}).get("stubtest", {})
assert isinstance(config, dict)
return config
def get_stubtest_strictness(
package_name: PackageName, *, typeshed_dir: Path | str
) -> StubtestStrictness:
"""Get the setting typeshed uses in CI when [stubtest][] is run on a certain package.
Parameters:
package_name: The name of the package to find the stubtest setting for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the stubtest setting.
Returns:
A member of the [`StubtestStrictness`](./#StubtestStrictness)
enumeration (see the docs on `StubtestStrictness` for details).
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_stubtest_strictness
>>> with tmpdir_typeshed() as typeshed:
... stdlib_setting = get_stubtest_strictness("stdlib", typeshed_dir=typeshed)
... gdb_setting = get_stubtest_strictness("gdb", typeshed_dir=typeshed)
>>> stdlib_setting
StubtestStrictness.ERROR_ON_MISSING_STUB
>>> help(_)
Help on StubtestStrictness in module typeshed_stats.gather:
<BLANKLINE>
StubtestStrictness.ERROR_ON_MISSING_STUB
Objects missing from the stub cause stubtest to emit an error in typeshed's CI.
<BLANKLINE>
>>> gdb_setting
StubtestStrictness.SKIPPED
"""
if package_name == "stdlib":
return StubtestStrictness.ERROR_ON_MISSING_STUB
match _get_stubtest_config(package_name, typeshed_dir):
case {"skip": True}:
return StubtestStrictness.SKIPPED
case {"ignore_missing_stub": True}:
return StubtestStrictness.MISSING_STUBS_IGNORED
case _:
return StubtestStrictness.ERROR_ON_MISSING_STUB
def get_stubtest_platforms(
package_name: PackageName, *, typeshed_dir: Path | str
) -> list[str]:
"""Get the list of platforms on which [stubtest][] is run in typeshed's CI.
Parameters:
package_name: The name of the package to find the stubtest setting for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the stubtest configuration.
Returns:
A list of strings describing platforms stubtest is run on.
The names correspond to the platform names
given by [sys.platform][] at runtime.
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_stubtest_platforms
>>> with tmpdir_typeshed() as typeshed:
... pywin_platforms = get_stubtest_platforms("pywin32", typeshed_dir=typeshed)
>>> pywin_platforms
['win32']
"""
if package_name == "stdlib":
return ["darwin", "linux", "win32"]
match _get_stubtest_config(package_name, typeshed_dir):
case {"skip": True}:
return []
case {"platforms": list() as platforms}:
return sorted(platforms)
case _:
return ["linux"]
def _allowlist_entries_in_file(path: Path) -> set[str]:
with path.open(encoding="utf-8") as file:
return {line.split("#")[0].strip() for line in file} - {""}
def get_stubtest_allowlist_length(
package_name: PackageName, *, typeshed_dir: Path | str
) -> int:
"""Get the number of unique "allowlist entries" typeshed uses in CI when [stubtest][] is run on a certain package.
An allowlist entry indicates a place in the stub where stubtest emits an error,
but typeshed has chosen to silence the error rather than "fix it".
Not all allowlist entries are bad:
sometimes there are good reasons to ignore an error emitted by stubtest.
Parameters:
package_name: The name of the package
to find the number of allowlist entries for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the number of stubtest allowlist entries.
Returns:
The number of allowlist entries for that package.
Duplicate entries in allowlists are removed.
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_stubtest_allowlist_length
>>> with tmpdir_typeshed() as typeshed:
... num_stdlib_allows = get_stubtest_allowlist_length("stdlib", typeshed_dir=typeshed)
... num_requests_allows = get_stubtest_allowlist_length("requests", typeshed_dir=typeshed)
>>> type(num_stdlib_allows)
<class 'int'>
>>> num_stdlib_allows > 0 and num_requests_allows > 0
True
"""
if package_name == "stdlib":
allowlist_dir = Path(typeshed_dir, "tests", "stubtest_allowlists")
combined_allowlist = chain.from_iterable(
_allowlist_entries_in_file(file) for file in allowlist_dir.glob("*.txt")
)
else:
allowlist_dir = Path(typeshed_dir, "stubs", package_name, "@tests")
if not allowlist_dir.exists():
return 0
combined_allowlist = chain.from_iterable(
_allowlist_entries_in_file(file)
for file in allowlist_dir.glob("stubtest_allowlist*.txt")
)
deduplicated_allowlist = set(combined_allowlist)
return len(deduplicated_allowlist)
@final
@attrs.define
class StubtestSettings:
"""Information on the settings under which [stubtest][] is run on a certain package."""
strictness: StubtestStrictness
platforms: list[str]
allowlist_length: int
def get_stubtest_settings(
package_name: PackageName, *, typeshed_dir: Path | str
) -> StubtestSettings:
"""Get the [stubtest][] settings for a certain stubs package in typeshed.
Parameters:
package_name: The name of the package to find the stubtest settings for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the stubtest settings.
Returns:
An instance of the [`StubtestSettings`](./#StubtestSettings) class.
"""
return StubtestSettings(
strictness=get_stubtest_strictness(package_name, typeshed_dir=typeshed_dir),
platforms=get_stubtest_platforms(package_name, typeshed_dir=typeshed_dir),
allowlist_length=get_stubtest_allowlist_length(
package_name, typeshed_dir=typeshed_dir
),
)
class PackageStatus(_NiceReprEnum):
"""The various states of freshness/staleness a stubs package can be in."""
STDLIB = (
"These are typeshed's stubs for the standard library. "
"Typeshed's stdlib stubs are generally fairly up to date, "
"and are tested against all currently supported Python versions "
"in typeshed's CI."
)
NOT_ON_PYPI = (
"The runtime package that these stubs are for doesn't exist on PyPI, "
"so whether or not these stubs are up to date or not is unknown."
)
OBSOLETE = (
"The runtime package has added inline type hints; "
"these typeshed stubs are now obsolete."
)
NO_LONGER_UPDATED = (
"The runtime package has not added type hints, "
"but these stubs are no longer updated by typeshed for some other reason."
)
OUT_OF_DATE = (
"These stubs may be out of date. "
"In typeshed's CI, [stubtest][] tests these stubs "
"against an older version of the runtime package "
"than the latest that's available."
)
UP_TO_DATE = (
"These stubs should be fairly up to date. In typeshed's CI, [stubtest][] "
"tests these stubs against the latest version of the runtime package "
"that's available."
)
_PypiData: TypeAlias = Annotated[
dict[str, Any], "JSON information from PyPI about the package"
]
async def _get_pypi_data(
package_name: PackageName, session: aiohttp.ClientSession | None
) -> _PypiData:
pypi_data_url = f"https://pypi.org/pypi/{urllib.parse.quote(package_name)}/json"
async with AsyncExitStack() as stack:
if session is None:
session = await stack.enter_async_context(aiohttp.ClientSession())
async with session.get(pypi_data_url) as response:
response.raise_for_status()
response_json = await response.json()
assert isinstance(response_json, dict)
return response_json
async def get_package_status(
package_name: PackageName,
*,
typeshed_dir: Path | str,
session: aiohttp.ClientSession | None = None,
) -> PackageStatus:
"""Retrieve information on how up to date a stubs package is.
If [stubtest][] tests these stubs against the latest version of the runtime package
in typeshed's CI, it's a fair bet that the stubs are relatively up to date.
If stubtest tests these stubs against an older version, however,
the stubs *may* be out of date.
!!! note
This function makes network requests to PyPI in order to determine what the
latest version of the runtime is, and then compares this against
the metadata of the stubs package.
Parameters:
package_name: The name of the stubs package to analyze.
typeshed_dir: A path pointing to a typeshed directory
in which to find the stubs package.
session: An [aiohttp.ClientSession][] instance,
to be used for making a network requests, or [None][]. If `None` is provided
for this argument, a new `aiohttp.ClientSession` instance will be
created to make the network request.
Returns:
A member of the [`PackageStatus`](./#PackageStatus) enumeration
(see the docs on `PackageStatus` for details).
Examples:
>>> import asyncio
>>> from typeshed_stats.gather import tmpdir_typeshed, get_package_status
>>> with tmpdir_typeshed() as typeshed:
... stdlib_status = asyncio.run(get_package_status("stdlib", typeshed_dir=typeshed))
... gdb_status = asyncio.run(get_package_status("gdb", typeshed_dir=typeshed))
...
>>> stdlib_status
PackageStatus.STDLIB
>>> help(_)
Help on PackageStatus in module typeshed_stats.gather:
<BLANKLINE>
PackageStatus.STDLIB
These are typeshed's stubs for the standard library. Typeshed's stdlib stubs are generally fairly up to date, and are tested against all currently supported Python versions in typeshed's CI.
<BLANKLINE>
>>> gdb_status
PackageStatus.NOT_ON_PYPI
"""
match package_name:
case "stdlib":
return PackageStatus.STDLIB
case "gdb":
return PackageStatus.NOT_ON_PYPI
case _:
pass
match metadata := _get_package_metadata(package_name, typeshed_dir):
case {"obsolete_since": _}:
return PackageStatus.OBSOLETE
case {"no_longer_updated": True}:
return PackageStatus.NO_LONGER_UPDATED
case _:
pass
typeshed_pinned_version = SpecifierSet(f"=={metadata['version']}")
pypi_data = await _get_pypi_data(package_name, session)
pypi_version = Version(pypi_data["info"]["version"])
status = "UP_TO_DATE" if pypi_version in typeshed_pinned_version else "OUT_OF_DATE"
return PackageStatus[status]
class UploadStatus(_NiceReprEnum):
"""Whether or not a stubs package is currently uploaded to PyPI."""
UPLOADED = "These stubs are currently uploaded to PyPI."
NOT_CURRENTLY_UPLOADED = "These stubs are not currently uploaded to PyPI."
def get_upload_status(
package_name: PackageName, *, typeshed_dir: Path | str
) -> UploadStatus:
"""Determine whether a certain package is currently uploaded to PyPI.
Parameters:
package_name: The name of the package to find the upload status for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the stubtest setting.
Returns:
A member of the [`UploadStatus`](./#UploadStatus) enumeration
(see the docs on `UploadStatus` for details).
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_upload_status
>>> with tmpdir_typeshed() as typeshed:
... stdlib_setting = get_upload_status("stdlib", typeshed_dir=typeshed)
... requests_setting = get_upload_status("requests", typeshed_dir=typeshed)
>>> stdlib_setting
UploadStatus.NOT_CURRENTLY_UPLOADED
>>> help(_)
Help on UploadStatus in module typeshed_stats.gather:
<BLANKLINE>
UploadStatus.NOT_CURRENTLY_UPLOADED
These stubs are not currently uploaded to PyPI.
<BLANKLINE>
>>> requests_setting
UploadStatus.UPLOADED
"""
if package_name == "stdlib":
return UploadStatus.NOT_CURRENTLY_UPLOADED
match _get_package_metadata(package_name, typeshed_dir):
case {"upload": False}:
return UploadStatus.NOT_CURRENTLY_UPLOADED
case _:
return UploadStatus.UPLOADED
def get_stub_distribution_name(
package_name: PackageName, *, typeshed_dir: Path | str
) -> str:
"""Get the name this stubs package is uploaded to PyPI under.
For the vast majority of packages in typeshed, this is `types-{runtime-name}`,
but there may be a small number of packages
that are uploaded under nonstandard names to PyPI.
Parameters:
package_name: The (runtime) name of the package
to find the stub distribution name for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the information.
Returns:
The name under which the stubs package is uploaded to PyPI.
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_stub_distribution_name
>>> with tmpdir_typeshed() as typeshed:
... requests_stub_dist_name = get_stub_distribution_name("requests", typeshed_dir=typeshed)
... pika_stub_dist_name = get_stub_distribution_name("pika", typeshed_dir=typeshed)
>>> requests_stub_dist_name
'types-requests'
>>> pika_stub_dist_name
'types-pika-ts'
"""
if package_name == "stdlib":
return "-"
match _get_package_metadata(package_name, typeshed_dir):
case {"stub_distribution": str() as stub_distribution}:
return stub_distribution
case _:
return f"types-{package_name}"
def get_number_of_lines_of_file(file_path: Path | str) -> int:
"""Get the total number of lines of code for a single stub file in typeshed.
Parameters:
file_path: A path to the file to analyse.
Returns:
The number of lines of code the stubs file contains,
excluding empty lines.
"""
with open(file_path, encoding="utf-8") as file:
return sum(1 for line in file if line.strip())
def get_package_size(package_name: PackageName, *, typeshed_dir: Path | str) -> int:
"""Get the total number of lines of code for a stubs package in typeshed.
Parameters:
package_name: The name of the stubs package to find the line number for.
typeshed_dir: A path pointing to a typeshed directory
in which to find the stubs package.
Returns:
The number of lines of code the stubs package contains,
excluding empty lines.
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_package_size
>>> with tmpdir_typeshed() as typeshed:
... mypy_extensions_size = get_package_size("mypy-extensions", typeshed_dir=typeshed)
...
>>> type(mypy_extensions_size) is int and mypy_extensions_size > 0
True
"""
return sum(
get_number_of_lines_of_file(file)
for file in _get_package_directory(package_name, typeshed_dir).rglob("*.pyi")
)
def _is_str_list(obj: object) -> TypeGuard[list[str]]:
return isinstance(obj, list) and all(isinstance(item, str) for item in obj)
class _ExcludeList(NamedTuple):
spec: PathSpec
pathlist: list[Path]
def _normalized_path(path: Path) -> str:
normalized_path = path.as_posix()
if path.is_dir():
normalized_path += "/"
return normalized_path
@lru_cache
def _get_pyright_excludelist(
*,
typeshed_dir: Path | str,
config_filename: Literal["pyrightconfig.json", "pyrightconfig.stricter.json"],
) -> _ExcludeList:
# Read the config file;
# do some pre-processing so that it can be passed to json.loads()
config_path = Path(typeshed_dir, config_filename)
with config_path.open(encoding="utf-8") as file:
# strip comments from the file
lines = [line for line in file if not line.strip().startswith("//")]
# strip trailing commas from the file
valid_json = re.sub(r",(\s*?[\}\]])", r"\1", "\n".join(lines))
pyright_config = json.loads(valid_json)
assert isinstance(pyright_config, dict)
excludelist: object = pyright_config.get("exclude", [])
assert _is_str_list(excludelist)
excludelist_as_paths = [Path(typeshed_dir, item) for item in excludelist]
return _ExcludeList(
PathSpec.from_lines(
GitWildMatchPattern,
[_normalized_path(item) for item in excludelist_as_paths],
),
excludelist_as_paths,
)
class PyrightSetting(_NiceReprEnum):
"""The various possible [pyright][] settings typeshed uses in CI."""
ENTIRELY_EXCLUDED = (
"All files in this stubs package "
"are excluded from the pyright check in typeshed's CI."
)
SOME_FILES_EXCLUDED = (
"Some files in this stubs package "
"are excluded from the pyright check in typeshed's CI."
)
NOT_STRICT = (
"This package is tested with pyright in typeshed's CI, "
"but all files in this stubs package "
"are excluded from the stricter pyright settings."
)
STRICT_ON_SOME_FILES = (
"Some files in this stubs package "
"are tested with the stricter pyright settings in typeshed's CI; "
"some are excluded from the stricter settings."
)
STRICT = (
"All files in this stubs package are tested with the stricter pyright settings "
"in typeshed's CI."
)
def _path_or_path_ancestor_is_listed(path: Path, spec: PathSpec) -> bool:
if spec.match_file(_normalized_path(path)):
return True
if not path.is_dir():
return False
for subpath in path.rglob("*"):
if not spec.match_file(_normalized_path(subpath)):
return False
else:
return True
def _child_of_path_is_listed(path: Path, path_list: Collection[Path]) -> bool:
return any(path in listed_path.parents for listed_path in path_list)
def get_pyright_setting_for_path(
file_path: Path | str, *, typeshed_dir: Path | str
) -> PyrightSetting:
"""Get the settings typeshed uses in CI when [pyright][] is run on a certain path.
Parameters:
file_path: The path to query.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the pyright setting.
Returns:
A member of the [`PyrightSetting`](./#PyrightSetting) enumeration
(see the docs on `PyrightSetting` for details).
"""
entirely_excluded_paths = _get_pyright_excludelist(
typeshed_dir=typeshed_dir, config_filename="pyrightconfig.json"
)
paths_excluded_from_stricter_check = _get_pyright_excludelist(
typeshed_dir=typeshed_dir, config_filename="pyrightconfig.stricter.json"
)
file_path = Path(typeshed_dir, file_path)
if _path_or_path_ancestor_is_listed(file_path, entirely_excluded_paths.spec):
return PyrightSetting.ENTIRELY_EXCLUDED
if _child_of_path_is_listed(file_path, entirely_excluded_paths.pathlist):
return PyrightSetting.SOME_FILES_EXCLUDED
if _path_or_path_ancestor_is_listed(
file_path, paths_excluded_from_stricter_check.spec
):
return PyrightSetting.NOT_STRICT
if _child_of_path_is_listed(file_path, paths_excluded_from_stricter_check.pathlist):
return PyrightSetting.STRICT_ON_SOME_FILES
return PyrightSetting.STRICT
def get_pyright_setting_for_package(
package_name: PackageName, *, typeshed_dir: Path | str
) -> PyrightSetting:
"""Get the settings typeshed uses in CI when [pyright][] is run on a certain package.
Parameters:
package_name: The name of the package to find the pyright setting for.
typeshed_dir: A path pointing to a typeshed directory,
from which to retrieve the pyright setting.
Returns:
A member of the [`PyrightSetting`](./#PyrightSetting) enumeration
(see the docs on `PyrightSetting` for details).
Examples:
>>> from typeshed_stats.gather import tmpdir_typeshed, get_pyright_setting_for_package
>>> with tmpdir_typeshed() as typeshed:
... stdlib_setting = get_pyright_setting_for_package("stdlib", typeshed_dir=typeshed)
...
>>> stdlib_setting
PyrightSetting.STRICT_ON_SOME_FILES
>>> help(_)
Help on PyrightSetting in module typeshed_stats.gather:
<BLANKLINE>
PyrightSetting.STRICT_ON_SOME_FILES
Some files in this stubs package are tested with the stricter pyright settings in typeshed's CI; some are excluded from the stricter settings.
<BLANKLINE>
"""
return get_pyright_setting_for_path(
file_path=_get_package_directory(package_name, typeshed_dir),
typeshed_dir=typeshed_dir,
)
@final
@attrs.define
class PackageInfo:
"""Statistics about a single stubs package in typeshed."""
package_name: PackageName
stub_distribution_name: str
extra_description: str | None
number_of_lines: int
package_status: PackageStatus
upload_status: UploadStatus
stubtest_settings: StubtestSettings
pyright_setting: PyrightSetting
annotation_stats: AnnotationStats
async def gather_stats_on_package(
package_name: PackageName,
*,
typeshed_dir: Path | str,