Skip to content

Commit a640ca6

Browse files
Resolve unsoundness caught by pytype --strict-none-binding.
PiperOrigin-RevId: 761353134
1 parent 7a3451c commit a640ca6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

tensorflow_privacy/privacy/optimizers/dp_optimizer_keras_sparse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def _distributed_apply(
310310
def increment_acc_iterations():
311311
# Always use locking when updating the steps, so we don't under-count
312312
# the steps (which could invalidate privacy accounting).
313+
assert self._acc_iterations is not None
313314
return self._acc_iterations.assign_add(
314315
1, use_locking=True, read_value=False
315316
)

tensorflow_privacy/privacy/privacy_tests/membership_inference_attack/data_structures.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ def __str__(self):
8282
f'Custom indices: train = {custom_train_indices}, '
8383
f'test = {custom_test_indices}, group_value = {slice_value}'
8484
)
85-
86-
return '%s=%s' % (self.feature.name, self.value)
85+
feature = self.feature
86+
assert feature is not None
87+
return '%s=%s' % (feature.name, self.value)
8788

8889

8990
@dataclasses.dataclass
@@ -427,6 +428,11 @@ def get_train_shape(self):
427428
return self.loss_train.shape
428429
if self.entropy_train is not None:
429430
return self.entropy_train.shape
431+
if self.logits_or_probs_train is None:
432+
raise ValueError(
433+
'logits_or_probs_train should not be None when loss and entropy are'
434+
' None.'
435+
)
430436
return self.logits_or_probs_train.shape
431437

432438
def get_test_shape(self):
@@ -435,6 +441,11 @@ def get_test_shape(self):
435441
return self.loss_test.shape
436442
if self.entropy_test is not None:
437443
return self.entropy_test.shape
444+
if self.logits_or_probs_test is None:
445+
raise ValueError(
446+
'logits_or_probs_test should not be None when loss and entropy are'
447+
' None.'
448+
)
438449
return self.logits_or_probs_test.shape
439450

440451
def get_train_size(self):
@@ -1041,7 +1052,9 @@ def calculate_pd_dataframe(self):
10411052
if slice_spec.entire_dataset:
10421053
slice_feature, slice_value = str(slice_spec), ''
10431054
else:
1044-
slice_feature, slice_value = slice_spec.feature.value, slice_spec.value
1055+
slice_spec_feature = slice_spec.feature
1056+
assert slice_spec_feature is not None
1057+
slice_feature, slice_value = slice_spec_feature.value, slice_spec.value
10451058
slice_features.append(str(slice_feature))
10461059
slice_values.append(str(slice_value))
10471060
data_size_train.append(attack_result.data_size.ntrain)
@@ -1091,6 +1104,7 @@ def summary(self, by_slices=False) -> str:
10911104

10921105
# Summary over all slices
10931106
max_auc_result_all = self.get_result_with_max_auc()
1107+
assert max_auc_result_all is not None
10941108
summary.append('Best-performing attacks over all slices')
10951109
summary.append(
10961110
' %s (with %d training and %d test examples) achieved an AUC of %.2f'
@@ -1105,6 +1119,7 @@ def summary(self, by_slices=False) -> str:
11051119
)
11061120

11071121
max_advantage_result_all = self.get_result_with_max_attacker_advantage()
1122+
assert max_advantage_result_all is not None
11081123
summary.append(
11091124
' %s (with %d training and %d test examples) achieved an advantage of'
11101125
' %.2f on slice %s'
@@ -1118,6 +1133,7 @@ def summary(self, by_slices=False) -> str:
11181133
)
11191134

11201135
max_ppv_result_all = self.get_result_with_max_ppv()
1136+
assert max_ppv_result_all is not None
11211137
summary.append(
11221138
' %s (with %d training and %d test examples) achieved a positive '
11231139
'predictive value of %.2f on slice %s' %
@@ -1126,6 +1142,7 @@ def summary(self, by_slices=False) -> str:
11261142
max_ppv_result_all.slice_spec))
11271143

11281144
max_epsilon_lower_bound_all = self.get_result_with_max_epsilon()
1145+
assert max_epsilon_lower_bound_all is not None
11291146
summary.append(
11301147
' %s (with %d training and %d test examples) achieved top-%d epsilon '
11311148
'lower bounds of %s on slice %s'
@@ -1149,6 +1166,7 @@ def summary(self, by_slices=False) -> str:
11491166
summary.append('\nBest-performing attacks over slice: \"%s\"' %
11501167
slice_str)
11511168
max_auc_result = results.get_result_with_max_auc()
1169+
assert max_auc_result is not None
11521170
summary.append(
11531171
' %s (with %d training and %d test examples) achieved an AUC of'
11541172
' %.2f'
@@ -1160,6 +1178,7 @@ def summary(self, by_slices=False) -> str:
11601178
)
11611179
)
11621180
max_advantage_result = results.get_result_with_max_attacker_advantage()
1181+
assert max_advantage_result is not None
11631182
summary.append(
11641183
' %s (with %d training and %d test examples) achieved an advantage'
11651184
' of %.2f'
@@ -1171,12 +1190,14 @@ def summary(self, by_slices=False) -> str:
11711190
)
11721191
)
11731192
max_ppv_result = results.get_result_with_max_ppv()
1193+
assert max_ppv_result is not None
11741194
summary.append(
11751195
' %s (with %d training and %d test examples) achieved a positive '
11761196
'predictive value of %.2f' %
11771197
(max_ppv_result.attack_type, max_ppv_result.data_size.ntrain,
11781198
max_ppv_result.data_size.ntest, max_ppv_result.get_ppv()))
11791199
max_epsilon_lower_bound_all = results.get_result_with_max_epsilon()
1200+
assert max_epsilon_lower_bound_all is not None
11801201
summary.append(
11811202
' %s (with %d training and %d test examples) achieved top-%d '
11821203
'epsilon lower bounds of %s'

0 commit comments

Comments
 (0)