Skip to content

Commit 6349398

Browse files
generatedunixname89002005307016meta-codesync[bot]
authored andcommitted
Enable Pyrefly in fbcode/aml/augly
Summary: Automated migration to enable Pyrefly type checking for `fbcode/aml/augly`. - Added `python.set_pyrefly(True)` to PACKAGE file - Suppressed pre-existing type errors Pyrefly is Meta's next-generation Python type checker, replacing Pyre. If you encounter issues, you can revert the PACKAGE change by removing the `python.set_pyrefly(True)` line. #pyreupgrade Differential Revision: D107126783 fbshipit-source-id: cee788e6240bffe2034211c2d2cb182877429073
1 parent f8c2ad9 commit 6349398

10 files changed

Lines changed: 81 additions & 8 deletions

File tree

augly/audio/functional.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,20 @@ def add_background_noise(
104104

105105
background_audio *= desired_bg_rms / bg_rms
106106

107+
# pyrefly: ignore [missing-attribute]
107108
while background_audio.shape[-1] < audio.shape[-1]:
109+
# pyrefly: ignore [missing-attribute]
108110
axis = 0 if background_audio.ndim == 1 else 1
109111
background_audio = np.concatenate(
110112
(background_audio, background_audio), axis=axis
111113
)
112114

113115
background_audio = (
116+
# pyrefly: ignore [unsupported-operation]
114117
background_audio[: audio.shape[-1]]
118+
# pyrefly: ignore [missing-attribute]
115119
if background_audio.ndim == 1
120+
# pyrefly: ignore [bad-index, unsupported-operation]
116121
else background_audio[:, : audio.shape[-1]]
117122
)
118123

augly/audio/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ def check_random_state(seed: RNGSeed | None) -> RNG:
8686
Otherwise raise ValueError.
8787
"""
8888
if seed is None or seed is np.random:
89+
# pyrefly: ignore [missing-attribute]
8990
return np.random.mtrand._rand
9091
if isinstance(seed, numbers.Integral):
92+
# pyrefly: ignore [bad-argument-type]
9193
return np.random.RandomState(cast(int, seed))
9294
if isinstance(seed, (np.random.RandomState, np.random.Generator)):
9395
return seed

augly/image/functional.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,7 @@ def overlay_random_text_with_background(
18421842
random.seed(seed)
18431843

18441844
if font_file is None:
1845+
# pyrefly: ignore [bad-assignment]
18451846
font_file = utils.SOCIAL_MEDIA_OVERLAY_FONTS
18461847

18471848
font_files = font_file if isinstance(font_file, list) else [font_file]
@@ -1877,6 +1878,7 @@ def overlay_random_text_with_background(
18771878

18781879
for overlay_idx in range(num_overlays):
18791880
cur_font_file = random.choice(font_files)
1881+
# pyrefly: ignore [bad-argument-type]
18801882
local_font_path = utils.pathmgr.get_local_path(cur_font_file)
18811883

18821884
text = random.choice(phrases)
@@ -2054,6 +2056,7 @@ def overlay_random_text_with_background(
20542056
def overlay_text(
20552057
image: str | Image.Image,
20562058
output_path: str | None = None,
2059+
# pyrefly: ignore [bad-function-definition]
20572060
text: list[int | list[int]] = utils.DEFAULT_TEXT_INDICES,
20582061
font_file: str = utils.FONT_PATH,
20592062
font_size: float = 0.15,
@@ -2472,6 +2475,7 @@ def perspective_transform(
24722475
func_kwargs = imutils.get_func_kwargs(metadata, locals())
24732476
src_mode = image.mode
24742477

2478+
# pyrefly: ignore [bad-argument-type]
24752479
rng = np.random.RandomState(seed) if seed is not None else np.random
24762480
width, height = image.size
24772481

@@ -2616,6 +2620,7 @@ def random_noise(
26162620
src_mode = image.mode
26172621

26182622
if seed is not None:
2623+
# pyrefly: ignore [bad-argument-type]
26192624
np.random.seed(seed=seed)
26202625

26212626
np_image = np.asarray(image).astype(np.float32)
@@ -3158,6 +3163,7 @@ def shuffle_pixels(
31583163
31593164
@returns: the augmented PIL Image
31603165
"""
3166+
# pyrefly: ignore [bad-argument-type]
31613167
np.random.seed(seed)
31623168

31633169
image = imutils.validate_and_load_image(image)
@@ -3307,6 +3313,7 @@ def split_and_shuffle(
33073313
33083314
@returns: the augmented PIL Image
33093315
"""
3316+
# pyrefly: ignore [bad-argument-type]
33103317
np.random.seed(seed)
33113318

33123319
image = imutils.validate_and_load_image(image)

augly/image/transforms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ def apply_transform(
14161416
class OverlayText(BaseTransform):
14171417
def __init__(
14181418
self,
1419+
# pyrefly: ignore [bad-function-definition]
14191420
text: list[int | list[int]] = utils.DEFAULT_TEXT_INDICES,
14201421
font_file: str = utils.FONT_PATH,
14211422
font_size: float = 0.15,
@@ -2643,6 +2644,7 @@ def apply_random_transform(
26432644
"""
26442645
return F.change_aspect_ratio(
26452646
image,
2647+
# pyrefly: ignore [bad-argument-type]
26462648
ratio=self.chosen_value,
26472649
metadata=metadata,
26482650
bboxes=bboxes,
@@ -2693,6 +2695,7 @@ def apply_random_transform(
26932695
"""
26942696
return F.blur(
26952697
image,
2698+
# pyrefly: ignore [bad-argument-type]
26962699
radius=self.chosen_value,
26972700
metadata=metadata,
26982701
bboxes=bboxes,
@@ -2743,6 +2746,7 @@ def apply_random_transform(
27432746
"""
27442747
return F.brightness(
27452748
image,
2749+
# pyrefly: ignore [bad-argument-type]
27462750
factor=self.chosen_value,
27472751
metadata=metadata,
27482752
bboxes=bboxes,
@@ -2905,6 +2909,7 @@ def apply_random_transform(
29052909
"""
29062910
return F.pixelization(
29072911
image,
2912+
# pyrefly: ignore [bad-argument-type]
29082913
ratio=self.chosen_value,
29092914
metadata=metadata,
29102915
bboxes=bboxes,
@@ -2969,6 +2974,7 @@ def apply_random_transform(
29692974
"""
29702975
return F.rotate(
29712976
image,
2977+
# pyrefly: ignore [bad-argument-type]
29722978
degrees=self.chosen_value,
29732979
expand=self.expand,
29742980
fill_color=self.fill_color,

augly/text/augmenters/case.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
class CaseChanger:
2121
def __init__(self, case: str, seed: int | None) -> None:
2222
self.rng: np.random.RandomState | types.ModuleType = (
23-
np.random.RandomState(seed) if seed is not None else np.random
23+
# pyrefly: ignore [bad-argument-type]
24+
np.random.RandomState(seed)
25+
if seed is not None
26+
else np.random
2427
)
2528
self.case = case
2629

augly/text/augmenters/contraction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(
5353
self.aug_p = aug_p
5454
self.contraction_mapping = self.get_mapping(mapping)
5555
self.max_contraction_length = max_contraction_length
56+
# pyrefly: ignore [bad-argument-type, bad-assignment]
5657
self.rng = np.random.RandomState(seed) if seed is not None else np.random
5758

5859
def get_mapping(self, mapping: str | dict[str, Any] | None) -> ContractionMapping:

augly/text/augmenters/word_replacement.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def substitute(self, data: str) -> str:
8484
filtered_word_idxes = self.pre_skip_aug(tokens)
8585

8686
if self.priority_words is None:
87+
# pyrefly: ignore [bad-assignment]
8788
self.priority_words = self.word_mapping.mapping.keys()
8889

8990
aug_word_idxes = set(

augly/video/augmenters/cv2/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def random_fonts(
104104
chars = [chr(c) for c in pickle.load(f)]
105105
else:
106106
chars = list(string.ascii_letters + string.punctuation)
107+
# pyrefly: ignore [invalid-yield]
107108
yield font, chars
108109

109110
def random_fontscales(

augly/video/functional.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ def insert_in_background(
821821
video_duration = float(video_info["duration"])
822822
width, height = video_info["width"], video_info["height"]
823823

824+
# pyrefly: ignore [bad-argument-type]
824825
rng = np.random.RandomState(seed) if seed is not None else np.random
825826

826827
video_paths = []
@@ -955,6 +956,7 @@ def insert_in_background_multiple(
955956
src_ids
956957
), "src_ids need to be specified for the main video and all additional videos."
957958
func_kwargs = helpers.get_func_kwargs(metadata, locals(), video_path)
959+
# pyrefly: ignore [bad-argument-type]
958960
rng = np.random.RandomState(seed) if seed is not None else np.random
959961

960962
local_path = utils.pathmgr.get_local_path(video_path)
@@ -1840,6 +1842,7 @@ def perspective_transform_and_shake(
18401842
)
18411843

18421844
duration = float(helpers.get_video_info(video_path)["duration"])
1845+
# pyrefly: ignore [bad-argument-type]
18431846
rng = np.random.RandomState(seed) if seed is not None else np.random
18441847

18451848
def get_dx_dy(frame_number: int) -> dict:
@@ -1989,6 +1992,7 @@ def replace_with_color_frames(
19891992
if output_path != video_path:
19901993
shutil.copy(video_path, output_path)
19911994
if metadata is not None:
1995+
# pyrefly: ignore [bad-argument-type]
19921996
helpers.get_metadata(metadata=metadata, **func_kwargs)
19931997
return output_path or video_path
19941998

@@ -2009,6 +2013,7 @@ def replace_with_color_frames(
20092013
if offset_factor == 0 and duration_factor == 1.0:
20102014
shutil.copy(color_path, output_path)
20112015
if metadata is not None:
2016+
# pyrefly: ignore [bad-argument-type]
20122017
helpers.get_metadata(metadata=metadata, **func_kwargs)
20132018
return output_path or video_path
20142019

@@ -2032,6 +2037,7 @@ def replace_with_color_frames(
20322037
)
20332038

20342039
if metadata is not None:
2040+
# pyrefly: ignore [bad-argument-type]
20352041
helpers.get_metadata(metadata=metadata, **func_kwargs)
20362042

20372043
return output_path or video_path

augly/video/transforms.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,14 @@ def apply_transform(
19511951
@returns: the path to the augmented video
19521952
"""
19531953
return F.resize(
1954-
video_path, output_path, self.height, self.width, metadata=metadata
1954+
# pyrefly: ignore [bad-argument-type]
1955+
video_path,
1956+
output_path,
1957+
# pyrefly: ignore [bad-argument-type]
1958+
self.height,
1959+
# pyrefly: ignore [bad-argument-type]
1960+
self.width,
1961+
metadata=metadata,
19551962
)
19561963

19571964

@@ -2345,7 +2352,12 @@ def apply_random_transform(
23452352
@returns: the path to the augmented video
23462353
"""
23472354
return F.change_aspect_ratio(
2348-
video_path, output_path, ratio=self.chosen_value, metadata=metadata
2355+
# pyrefly: ignore [bad-argument-type]
2356+
video_path,
2357+
output_path,
2358+
# pyrefly: ignore [bad-argument-type]
2359+
ratio=self.chosen_value,
2360+
metadata=metadata,
23492361
)
23502362

23512363

@@ -2385,7 +2397,12 @@ def apply_random_transform(
23852397
@returns: the path to the augmented video
23862398
"""
23872399
return F.blur(
2388-
video_path, output_path, sigma=self.chosen_value, metadata=metadata
2400+
# pyrefly: ignore [bad-argument-type]
2401+
video_path,
2402+
output_path,
2403+
# pyrefly: ignore [bad-argument-type]
2404+
sigma=self.chosen_value,
2405+
metadata=metadata,
23892406
)
23902407

23912408

@@ -2427,6 +2444,7 @@ def apply_random_transform(
24272444
return F.brightness(
24282445
video_path,
24292446
output_path,
2447+
# pyrefly: ignore [bad-argument-type]
24302448
level=self.chosen_value,
24312449
metadata=metadata,
24322450
)
@@ -2468,7 +2486,12 @@ def apply_random_transform(
24682486
@returns: the path to the augmented video
24692487
"""
24702488
return F.contrast(
2471-
video_path, output_path, level=self.chosen_value, metadata=metadata
2489+
# pyrefly: ignore [bad-argument-type]
2490+
video_path,
2491+
output_path,
2492+
# pyrefly: ignore [bad-argument-type]
2493+
level=self.chosen_value,
2494+
metadata=metadata,
24722495
)
24732496

24742497

@@ -2577,6 +2600,7 @@ def apply_random_transform(
25772600
return F.encoding_quality(
25782601
video_path,
25792602
output_path,
2603+
# pyrefly: ignore [bad-argument-type]
25802604
quality=int(self.chosen_value),
25812605
metadata=metadata,
25822606
)
@@ -2617,6 +2641,7 @@ def apply_random_transform(
26172641
26182642
@returns: the path to the augmented video
26192643
"""
2644+
# pyrefly: ignore [bad-argument-type]
26202645
return F.fps(video_path, output_path, fps=self.chosen_value, metadata=metadata)
26212646

26222647

@@ -2654,7 +2679,12 @@ def apply_random_transform(
26542679
@returns: the path to the augmented video
26552680
"""
26562681
return F.add_noise(
2657-
video_path, output_path, level=int(self.chosen_value), metadata=metadata
2682+
# pyrefly: ignore [bad-argument-type]
2683+
video_path,
2684+
output_path,
2685+
# pyrefly: ignore [bad-argument-type]
2686+
level=int(self.chosen_value),
2687+
metadata=metadata,
26582688
)
26592689

26602690

@@ -2698,6 +2728,7 @@ def apply_random_transform(
26982728
return F.pixelization(
26992729
video_path,
27002730
output_path,
2731+
# pyrefly: ignore [bad-argument-type]
27012732
ratio=self.chosen_value,
27022733
metadata=metadata,
27032734
)
@@ -2737,7 +2768,12 @@ def apply_random_transform(
27372768
@returns: the path to the augmented video
27382769
"""
27392770
return F.rotate(
2740-
video_path, output_path, degrees=self.chosen_value, metadata=metadata
2771+
# pyrefly: ignore [bad-argument-type]
2772+
video_path,
2773+
output_path,
2774+
# pyrefly: ignore [bad-argument-type]
2775+
degrees=self.chosen_value,
2776+
metadata=metadata,
27412777
)
27422778

27432779

@@ -2781,5 +2817,10 @@ def apply_random_transform(
27812817
@returns: the path to the augmented video
27822818
"""
27832819
return F.change_video_speed(
2784-
video_path, output_path, factor=self.chosen_value, metadata=metadata
2820+
# pyrefly: ignore [bad-argument-type]
2821+
video_path,
2822+
output_path,
2823+
# pyrefly: ignore [bad-argument-type]
2824+
factor=self.chosen_value,
2825+
metadata=metadata,
27852826
)

0 commit comments

Comments
 (0)