Skip to content

Commit a2b81c3

Browse files
Chris Elionandrewcoh
andauthored
Uncomment obsolete attributes, fix warnings (#4771) (#4774)
* uncomment obsolete attributes, fix warnings * Apply suggestions from code review Co-authored-by: andrewcoh <[email protected]> Co-authored-by: andrewcoh <[email protected]> Co-authored-by: andrewcoh <[email protected]>
1 parent 8b9e953 commit a2b81c3

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public override int GetHashCode()
156156
/// <param name="destination">A float array to pack actions into whose length is greater than or
157157
/// equal to the addition of the Lengths of this objects <see cref="ContinuousActions"/> and
158158
/// <see cref="DiscreteActions"/> segments.</param>
159-
/// [Obsolete("PackActions has been deprecated.")]
159+
[Obsolete("PackActions has been deprecated.")]
160160
public void PackActions(in float[] destination)
161161
{
162162
Debug.Assert(destination.Length >= ContinuousActions.Length + DiscreteActions.Length,

com.unity.ml-agents/Runtime/Agent.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,11 @@ public virtual void Initialize() { }
886886
/// <seealso cref="IActionReceiver.OnActionReceived"/>
887887
public virtual void Heuristic(in ActionBuffers actionsOut)
888888
{
889-
// For backward compatibility
889+
// Disable deprecation warnings so we can call the legacy overload.
890+
#pragma warning disable CS0618
891+
892+
// The default implementation of Heuristic calls the
893+
// obsolete version for backward compatibility
890894
switch (m_PolicyFactory.BrainParameters.VectorActionSpaceType)
891895
{
892896
case SpaceType.Continuous:
@@ -904,6 +908,8 @@ public virtual void Heuristic(in ActionBuffers actionsOut)
904908
actionsOut.ContinuousActions.Clear();
905909
break;
906910
}
911+
#pragma warning restore CS0618
912+
907913
}
908914

909915
/// <summary>
@@ -1151,7 +1157,10 @@ public virtual void WriteDiscreteActionMask(IDiscreteActionMask actionMask)
11511157
{
11521158
m_ActionMasker = new DiscreteActionMasker(actionMask);
11531159
}
1160+
// Disable deprecation warnings so we can call the legacy overload.
1161+
#pragma warning disable CS0618
11541162
CollectDiscreteActionMasks(m_ActionMasker);
1163+
#pragma warning restore CS0618
11551164
}
11561165

11571166
/// <summary>
@@ -1232,7 +1241,10 @@ public virtual void OnActionReceived(ActionBuffers actions)
12321241
{
12331242
m_LegacyActionCache = Array.ConvertAll(actions.DiscreteActions.Array, x => (float)x);
12341243
}
1244+
// Disable deprecation warnings so we can call the legacy overload.
1245+
#pragma warning disable CS0618
12351246
OnActionReceived(m_LegacyActionCache);
1247+
#pragma warning restore CS0618
12361248
}
12371249

12381250
/// <summary>

com.unity.ml-agents/Runtime/Agent.deprecated.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public partial class Agent
99
/// Deprecated, use <see cref="WriteDiscreteActionMask"/> instead.
1010
/// </summary>
1111
/// <param name="actionMasker"></param>
12+
[Obsolete("CollectDiscreteActionMasks has been deprecated, please use WriteDiscreteActionMask.")]
1213
public virtual void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
1314
{
1415
}
@@ -17,6 +18,7 @@ public virtual void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker
1718
/// Deprecated, use <see cref="Heuristic(in ActionBuffers)"/> instead.
1819
/// </summary>
1920
/// <param name="actionsOut"></param>
21+
[Obsolete("The float[] version of Heuristic has been deprecated, please use the ActionBuffers version instead.")]
2022
public virtual void Heuristic(float[] actionsOut)
2123
{
2224
Debug.LogWarning("Heuristic method called but not implemented. Returning placeholder actions.");
@@ -27,6 +29,7 @@ public virtual void Heuristic(float[] actionsOut)
2729
/// Deprecated, use <see cref="OnActionReceived(ActionBuffers)"/> instead.
2830
/// </summary>
2931
/// <param name="vectorAction"></param>
32+
[Obsolete("The float[] version of OnActionReceived has been deprecated, please use the ActionBuffers version instead.")]
3033
public virtual void OnActionReceived(float[] vectorAction) { }
3134

3235
/// <summary>
@@ -36,7 +39,7 @@ public virtual void OnActionReceived(float[] vectorAction) { }
3639
/// The last action that was decided by the Agent (or null if no decision has been made).
3740
/// </returns>
3841
/// <seealso cref="OnActionReceived(ActionBuffers)"/>
39-
// [Obsolete("GetAction has been deprecated, please use GetStoredActionBuffers, Or GetStoredDiscreteActions.")]
42+
[Obsolete("GetAction has been deprecated, please use GetStoredActionBuffers instead.")]
4043
public float[] GetAction()
4144
{
4245
var storedAction = m_Info.storedVectorActions;

com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public static List<ObservationSummary> GetObservationSummaries(this AgentInfoAct
101101
/// <param name="isTraining">Whether or not the Brain is training.</param>
102102
public static BrainParametersProto ToProto(this BrainParameters bp, string name, bool isTraining)
103103
{
104+
// Disable deprecation warnings so we can set legacy fields
105+
#pragma warning disable CS0618
104106
var brainParametersProto = new BrainParametersProto
105107
{
106108
VectorActionSpaceTypeDeprecated = (SpaceTypeProto)bp.VectorActionSpaceType,
@@ -116,6 +118,7 @@ public static BrainParametersProto ToProto(this BrainParameters bp, string name,
116118
{
117119
brainParametersProto.VectorActionDescriptionsDeprecated.AddRange(bp.VectorActionDescriptions);
118120
}
121+
#pragma warning restore CS0618
119122
return brainParametersProto;
120123
}
121124

com.unity.ml-agents/Runtime/Policies/BrainParameters.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public ActionSpec ActionSpec
7878
/// the action.
7979
/// For the discrete action space: the number of branches in the action space.
8080
/// </value>
81-
/// [Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")]
81+
[Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")]
8282
[FormerlySerializedAs("vectorActionSize")]
8383
public int[] VectorActionSize = new[] { 1 };
8484

@@ -91,7 +91,7 @@ public ActionSpec ActionSpec
9191
/// <summary>
9292
/// (Deprecated) Defines if the action is discrete or continuous.
9393
/// </summary>
94-
/// [Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")]
94+
[Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")]
9595
[FormerlySerializedAs("vectorActionSpaceType")]
9696
public SpaceType VectorActionSpaceType = SpaceType.Discrete;
9797

@@ -102,7 +102,7 @@ public ActionSpec ActionSpec
102102
/// <summary>
103103
/// (Deprecated) The number of actions specified by this Brain.
104104
/// </summary>
105-
/// [Obsolete("NumActions has been deprecated, please use ActionSpec instead.")]
105+
[Obsolete("NumActions has been deprecated, please use ActionSpec instead.")]
106106
public int NumActions
107107
{
108108
get
@@ -117,6 +117,8 @@ public int NumActions
117117
/// <returns> A new BrainParameter object with the same values as the original.</returns>
118118
public BrainParameters Clone()
119119
{
120+
// Disable deprecation warnings so we can read/write the old fields.
121+
#pragma warning disable CS0618
120122
return new BrainParameters
121123
{
122124
VectorObservationSize = VectorObservationSize,
@@ -126,13 +128,16 @@ public BrainParameters Clone()
126128
VectorActionSize = (int[])VectorActionSize.Clone(),
127129
VectorActionSpaceType = VectorActionSpaceType,
128130
};
131+
#pragma warning restore CS0618
129132
}
130133

131134
/// <summary>
132135
/// Propogate ActionSpec fields from deprecated fields
133136
/// </summary>
134137
private void UpdateToActionSpec()
135138
{
139+
// Disable deprecation warnings so we can read the old fields.
140+
#pragma warning disable CS0618
136141
if (!hasUpgradedBrainParametersWithActionSpec
137142
&& m_ActionSpec.NumContinuousActions == 0
138143
&& m_ActionSpec.BranchSizes == null)
@@ -149,13 +154,17 @@ private void UpdateToActionSpec()
149154
}
150155
}
151156
hasUpgradedBrainParametersWithActionSpec = true;
157+
#pragma warning restore CS0618
152158
}
153159

154160
/// <summary>
155161
/// Sync values in ActionSpec fields to deprecated fields
156162
/// </summary>
157163
private void SyncDeprecatedActionFields()
158164
{
165+
// Disable deprecation warnings so we can read the old fields.
166+
#pragma warning disable CS0618
167+
159168
if (m_ActionSpec.NumContinuousActions == 0)
160169
{
161170
VectorActionSize = (int[])ActionSpec.BranchSizes.Clone();
@@ -170,6 +179,7 @@ private void SyncDeprecatedActionFields()
170179
{
171180
VectorActionSize = null;
172181
}
182+
#pragma warning restore CS0618
173183
}
174184

175185
/// <summary>

com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,16 @@ public void TestHeuristicPolicyStepsSensors()
711711
Assert.AreEqual(numSteps, agent1.sensor1.numWriteCalls);
712712
Assert.AreEqual(numSteps, agent1.sensor2.numCompressedCalls);
713713

714+
// Disable deprecation warnings so we can read/write the old fields.
715+
#pragma warning disable CS0618
716+
714717
// Make sure the Heuristic method read the observation and set the action
715718
Assert.AreEqual(agent1.collectObservationsCallsForEpisode, agent1.GetAction()[0]);
719+
Assert.AreEqual(
720+
agent1.collectObservationsCallsForEpisode,
721+
agent1.GetStoredActionBuffers().ContinuousActions[0]
722+
);
723+
#pragma warning restore CS0618
716724
}
717725
}
718726

0 commit comments

Comments
 (0)