Skip to content

Commit

Permalink
Merge pull request #10 from yann-gael/fixlambda
Browse files Browse the repository at this point in the history
Fixlambda
  • Loading branch information
Ducasse authored May 16, 2024
2 parents a108c84 + ff6fcae commit ea39c2f
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 35 deletions.
60 changes: 60 additions & 0 deletions src/Famix-Python-Entities/FamixPythonClass.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
| Relation | Origin | Opposite | Type | Comment |
|---|
| `annotationInstances` | `FamixTWithAnnotationInstances` | `annotatedEntity` | `FamixTAnnotationInstance` | This property corresponds to the set of annotations associated to the entity|
| `instancedClasses` | `FamixPythonClass` | `metaclass` | `FamixPythonClass` | |
| `metaclass` | `FamixPythonClass` | `instancedClasses` | `FamixPythonClass` | |
| `receivingInvocations` | `FamixTInvocationsReceiver` | `receiver` | `FamixTInvocation` | List of invocations performed on this entity (considered as the receiver)|
| `sourceAnchor` | `FamixTSourceEntity` | `element` | `FamixTSourceAnchor` | SourceAnchor entity linking to the original source code for this entity|
| `typedEntities` | `FamixTType` | `declaredType` | `FamixTTypedEntity` | Entities that have this type as declaredType|
Expand All @@ -41,6 +43,7 @@
| Name | Type | Default value | Comment |
|---|
| `isMetaclass` | `Boolean` | nil | Whether this class is a metaclass|
| `isStub` | `Boolean` | false | Flag true if the entity attributes are incomplete, either because the entity is missing or not imported.|
| `name` | `String` | nil | Basic name of the entity, not full reference.|
Expand All @@ -50,6 +53,11 @@ Class {
#superclass : #FamixPythonType,
#traits : 'FamixTClass + FamixTImportable + FamixTWithAnnotationInstances + FamixTWithImports',
#classTraits : 'FamixTClass classTrait + FamixTImportable classTrait + FamixTWithAnnotationInstances classTrait + FamixTWithImports classTrait',
#instVars : [
'#isMetaclass => FMProperty',
'#metaclass => FMOne type: #FamixPythonClass opposite: #instancedClasses',
'#instancedClasses => FMMany type: #FamixPythonClass opposite: #metaclass'
],
#category : #'Famix-Python-Entities-Entities'
}

Expand All @@ -61,3 +69,55 @@ FamixPythonClass class >> annotation [
<generated>
^ self
]

{ #category : #adding }
FamixPythonClass >> addInstancedClass: anObject [
<generated>
^ self instancedClasses add: anObject
]

{ #category : #accessing }
FamixPythonClass >> instancedClasses [
"Relation named: #instancedClasses type: #FamixPythonClass opposite: #metaclass"

<generated>
<derived>
^ instancedClasses
]

{ #category : #accessing }
FamixPythonClass >> instancedClasses: anObject [

<generated>
instancedClasses value: anObject
]

{ #category : #accessing }
FamixPythonClass >> isMetaclass [

<FMProperty: #isMetaclass type: #Boolean>
<generated>
<FMComment: 'Whether this class is a metaclass'>
^ isMetaclass
]

{ #category : #accessing }
FamixPythonClass >> isMetaclass: anObject [
<generated>
isMetaclass := anObject
]

{ #category : #accessing }
FamixPythonClass >> metaclass [
"Relation named: #metaclass type: #FamixPythonClass opposite: #instancedClasses"

<generated>
^ metaclass
]

{ #category : #accessing }
FamixPythonClass >> metaclass: anObject [

<generated>
metaclass := anObject
]
7 changes: 7 additions & 0 deletions src/Famix-Python-Entities/FamixPythonEntity.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ FamixPythonEntity >> isClass [
^ false
]

{ #category : #testing }
FamixPythonEntity >> isFileAnchor [

<generated>
^ false
]

{ #category : #testing }
FamixPythonEntity >> isFunction [

Expand Down
11 changes: 11 additions & 0 deletions src/Famix-Python-Generator/FamixPythonGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ FamixPythonGenerator >> defineProperties [
(import property: #fromName type: #String) comment: '"from a import b", fromName is a'.
(import property: #entityName type: #String) comment: '"from a import b", entityName is b'.
(import property: #asName type: #String) comment: '"import b as c", asName is c'.

(class property: #isMetaclass type: #Boolean) comment: 'Whether this class is a metaclass'
]

{ #category : #definition }
FamixPythonGenerator >> defineRelations [

super defineRelations.

((class property: #metaclass) comment: '')
*- ((class property: #instancedClasses) comment: '')
]
186 changes: 165 additions & 21 deletions src/MoosePy/MSEPythonImporterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,61 @@ MSEPythonImporterTest >> testComentIsNotAssignment [
self deny: smaccAST statements anyOne stmts anyOne isAssignment
]

{ #category : #'tests - decorators' }
MSEPythonImporterTest >> testDecoratorsFunctionSingleDecorator [
"https://realpython.com/primer-on-python-decorators/"

| famix |
importer accept: (self parseCode: '
@decorator
def say_whee():
print("Whee!")
').

famix := (importer model allWithType: FamixPythonFunction) first.
self assert: famix isNotNil.
famix := famix annotationInstances.
self assert: famix size equals: 1.
famix := famix first.
self assert: famix annotatedEntity name equals: 'say_whee'.
]

{ #category : #'tests - decorators' }
MSEPythonImporterTest >> testDecoratorsMethodSingleDecorator [

| famix |
importer accept: (self parseCode: '
class A:
@classmethod
def foo:
pass
').

famix := importer classNamed: 'A'.
self assert: famix isNotNil.
famix := (famix methods first).
self assert: famix name equals: 'foo'.
famix := famix annotationInstances.
self assert: famix size equals: 1.
famix := famix first.
self assert: famix annotatedEntity name equals: 'foo'.
]

{ #category : #'tests - decorators' }
MSEPythonImporterTest >> testDecoratorsSanityCheck [

importer accept: (self parseCode: '
class A:
@classmethod
def foo:
pass
').

self
assert: (importer model allWithType: FamixPythonMethod) size
equals: 1
]

{ #category : #'tests - extensions' }
MSEPythonImporterTest >> testDottedNamedAssignmentIsAssignment [

Expand Down Expand Up @@ -202,6 +257,44 @@ MSEPythonImporterTest >> testFileIsOk [

]

{ #category : #'tests - lambdas' }
MSEPythonImporterTest >> testFunctionNoLambda [

| famix |
importer accept: (self parseCode: '
def identity:
print("identify")
').

self assert: (importer model allWithType: FamixPythonFunction) size equals: 1.

famix := importer functionNamed: 'identity'.
self assert: famix isNotNil.
self assert: famix functions size equals: 0.

]

{ #category : #'tests - lambdas' }
MSEPythonImporterTest >> testFunctionSingleLambda [

| famix |
importer accept: (self parseCode: '
def identity:
lambda x : x
').

self assert: (importer model allWithType: FamixPythonFunction) size equals: 2.

famix := importer functionNamed: 'identity'.
self assert: famix isNotNil.
self assert: famix functions size equals: 1.

famix := famix functions anyOne.
self assert: famix name equals: ''.
self assert: famix functionOwner name equals: 'identity'.

]

{ #category : #'tests - module' }
MSEPythonImporterTest >> testImport [

Expand Down Expand Up @@ -400,27 +493,6 @@ MSEPythonImporterTest >> testImportFunctions [
self assert: (importer functionNamed: 'printGrid') isNotNil
]

{ #category : #tests }
MSEPythonImporterTest >> testImportLambda [

| famix |
importer accept: (self parseCode: '
def identity:
lambda x : x
').

self assert: (importer model allWithType: FamixPythonFunction) size equals: 2.

famix := importer functionNamed: 'identity'.
self assert: famix isNotNil.
self assert: famix functions size equals: 1.

famix := famix functions anyOne.
self assert: famix name equals: ''.
self assert: famix functionOwner name equals: 'identity'.

]

{ #category : #tests }
MSEPythonImporterTest >> testImportMethods [

Expand Down Expand Up @@ -716,6 +788,30 @@ x //= 12
self assert: smaccAST statements anyOne stmts anyOne isAssignment
]

{ #category : #'tests - lambdas' }
MSEPythonImporterTest >> testMultiFunctionsMultiLambdas [

| famix |
importer accept: (self parseCode: '
def identity:
lambda x : x
def identity2:
lambda x : x
lambda x : x
').

self assert: (importer model allWithType: FamixPythonFunction) size equals: 5.

famix := importer functionNamed: 'identity'.
self assert: famix isNotNil.
self assert: famix functions size equals: 1.

famix := famix functions anyOne.
self assert: famix name equals: ''.
self assert: famix functionOwner name equals: 'identity'.

]

{ #category : #'tests - files' }
MSEPythonImporterTest >> testParseWithFile [

Expand All @@ -725,6 +821,54 @@ MSEPythonImporterTest >> testParseWithFile [
self assert: doc filename basename equals: 'sprite_collect_blocks.py'
]

{ #category : #'tests - metaclasses' }
MSEPythonImporterTest >> testSSingleClassExplicitSuperclass [

| classS classA |
importer accept: (self parseCode: '
class S:
pass
class A(S):
def foo:
pass
').

self
assert: (importer model allWithType: FamixPythonClass) size
equals: 3.

classS := importer classNamed: 'S'.
self assert: classS isNotNil.
self assert: classS superInheritances size equals: 1.

classA := importer classNamed: 'A'.
self assert: classA isNotNil.
self assert: classA superInheritances size equals: 1
]

{ #category : #'tests - metaclasses' }
MSEPythonImporterTest >> testSSingleClassingleMetaclass [

| classA classMeta |
importer accept: (self parseCode: '
class Meta(type):
pass
class A(metaclass=Meta):
def foo:
pass
').

self
assert: (importer model allWithType: FamixPythonClass) size
equals: 3.

classA := importer classNamed: 'A'.
self assert: classA isNotNil.
classMeta := importer classNamed: 'Meta'.
self assert: classMeta isNotNil.

]

{ #category : #tests }
MSEPythonImporterTest >> testSameAnnotationType [

Expand Down
Loading

0 comments on commit ea39c2f

Please sign in to comment.