66"""
77
88from abc import abstractmethod
9+ from typing import Union
910
1011
1112class Specification :
@@ -28,60 +29,60 @@ class CompositeSpecification(Specification):
2829 def is_satisfied_by (self , candidate ):
2930 pass
3031
31- def and_specification (self , candidate ) :
32+ def and_specification (self , candidate : "Specification" ) -> "AndSpecification" :
3233 return AndSpecification (self , candidate )
3334
34- def or_specification (self , candidate ) :
35+ def or_specification (self , candidate : "Specification" ) -> "OrSpecification" :
3536 return OrSpecification (self , candidate )
3637
37- def not_specification (self ):
38+ def not_specification (self ) -> "NotSpecification" :
3839 return NotSpecification (self )
3940
4041
4142class AndSpecification (CompositeSpecification ):
42- def __init__ (self , one , other ) :
43+ def __init__ (self , one : "Specification" , other : "Specification" ) -> None :
4344 self ._one : Specification = one
4445 self ._other : Specification = other
4546
46- def is_satisfied_by (self , candidate ) :
47+ def is_satisfied_by (self , candidate : Union [ "User" , str ]) -> bool :
4748 return bool (
4849 self ._one .is_satisfied_by (candidate )
4950 and self ._other .is_satisfied_by (candidate )
5051 )
5152
5253
5354class OrSpecification (CompositeSpecification ):
54- def __init__ (self , one , other ) :
55+ def __init__ (self , one : "Specification" , other : "Specification" ) -> None :
5556 self ._one : Specification = one
5657 self ._other : Specification = other
5758
58- def is_satisfied_by (self , candidate ):
59+ def is_satisfied_by (self , candidate : Union [ "User" , str ] ):
5960 return bool (
6061 self ._one .is_satisfied_by (candidate )
6162 or self ._other .is_satisfied_by (candidate )
6263 )
6364
6465
6566class NotSpecification (CompositeSpecification ):
66- def __init__ (self , wrapped ):
67+ def __init__ (self , wrapped : "Specification" ):
6768 self ._wrapped : Specification = wrapped
6869
69- def is_satisfied_by (self , candidate ):
70+ def is_satisfied_by (self , candidate : Union [ "User" , str ] ):
7071 return bool (not self ._wrapped .is_satisfied_by (candidate ))
7172
7273
7374class User :
74- def __init__ (self , super_user = False ):
75+ def __init__ (self , super_user : bool = False ) -> None :
7576 self .super_user = super_user
7677
7778
7879class UserSpecification (CompositeSpecification ):
79- def is_satisfied_by (self , candidate ) :
80+ def is_satisfied_by (self , candidate : Union [ "User" , str ]) -> bool :
8081 return isinstance (candidate , User )
8182
8283
8384class SuperUserSpecification (CompositeSpecification ):
84- def is_satisfied_by (self , candidate ) :
85+ def is_satisfied_by (self , candidate : "User" ) -> bool :
8586 return getattr (candidate , "super_user" , False )
8687
8788
0 commit comments