File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -801,10 +801,10 @@ impl<'a> BindingsBuilder<'a> {
801801 }
802802
803803 fn check_private_attribute_usage ( & mut self , attr : & ExprAttribute ) {
804- if self . scopes . in_function_scope ( ) {
804+ if ! Ast :: is_mangled_attr ( & attr . attr . id ) {
805805 return ;
806806 }
807- if Ast :: is_mangled_attr ( & attr . attr . id ) {
807+ if ! self . scopes . in_function_scope ( ) {
808808 self . error (
809809 attr. attr . range ,
810810 ErrorInfo :: Kind ( ErrorKind :: NoAccess ) ,
@@ -813,6 +813,17 @@ impl<'a> BindingsBuilder<'a> {
813813 attr. attr. id
814814 ) ,
815815 ) ;
816+ return ;
817+ }
818+ if self . scopes . current_method_and_class ( ) . is_none ( ) {
819+ self . error (
820+ attr. attr . range ,
821+ ErrorInfo :: Kind ( ErrorKind :: NoAccess ) ,
822+ format ! (
823+ "Private attribute `{}` can only be accessed from methods" ,
824+ attr. attr. id
825+ ) ,
826+ ) ;
816827 }
817828 }
818829
Original file line number Diff line number Diff line change @@ -1259,8 +1259,13 @@ exposed = A.__secret # E: Private attribute `__secret` cannot be accessed outsi
12591259class B:
12601260 leaked = A.__secret # E: Private attribute `__secret` cannot be accessed outside a function
12611261
1262- def ok(a: A) -> int:
1263- return A.__secret # OK
1262+ def fn_bad(a: A) -> int:
1263+ return a.__secret # E: Private attribute `__secret` can only be accessed from methods
1264+
1265+ class C:
1266+ __secret: int = 0
1267+ def good(self) -> int:
1268+ return self.__secret # OK
12641269"# ,
12651270) ;
12661271
You can’t perform that action at this time.
0 commit comments